class SiteAuditCheckCodebasePhpMessDetection in Site Audit 8.2
Class SiteAuditCheckCodebasePhpMessDetection.
Hierarchy
Expanded class hierarchy of SiteAuditCheckCodebasePhpMessDetection
File
- Check/
Codebase/ PhpMessDetection.php, line 12 - Contains \SiteAudit\Check\Codebase\PhpMessDetection.
View source
class SiteAuditCheckCodebasePhpMessDetection extends SiteAuditCheckAbstract {
/**
* Implements \SiteAudit\Check\Abstract\getLabel().
*/
public function getLabel() {
return dt('PHP Mess Detection');
}
/**
* Implements \SiteAudit\Check\Abstract\getDescription().
*/
public function getDescription() {
return dt('Check custom code for possible bugs and other suboptimal code.');
}
/**
* Implements \SiteAudit\Check\Abstract\getResultFail().
*/
public function getResultFail() {
return dt('Cannot check for suboptimal code; an invalid custom code path was specified!');
}
/**
* Implements \SiteAudit\Check\Abstract\getResultInfo().
*/
public function getResultInfo() {
if (isset($this->registry['phpmd_path_error'])) {
return dt('Missing phpmd.');
}
return dt('Cannot check for suboptimal code; no custom code path specified.');
}
/**
* Implements \SiteAudit\Check\Abstract\getResultPass().
*/
public function getResultPass() {
return dt('Custom Code does not violate any PHP Mess Detector rule.');
}
/**
* Implements \SiteAudit\Check\Abstract\getResultWarn().
*/
public function getResultWarn() {
$ret_val = '';
if (drush_get_option('html') == TRUE) {
$ret_val .= '<table class="table table-condensed">';
$ret_val .= '<thead><tr><th>' . dt('Line') . '</th><th>' . dt('Type') . '</th><th>' . dt('Action') . '</th></tr></thead>';
foreach ($this->registry['phpmd_out'] as $filename => $violations) {
$ret_val .= "<tr align='center'><td colspan='3'>File: {$filename}</td></tr>";
foreach ($violations as $violation) {
$begin = $violation['beginline'];
$end = $violation['endline'];
$rule = $violation['rule'];
$url = $violation['externalInfoUrl'];
$ret_val .= "<tr><td>{$begin} to {$end}</td><td><a href='{$url}'>{$rule}</a></td><td>{$violation}</td></tr>";
}
}
$ret_val .= '</table>';
}
else {
$rows = 0;
foreach ($this->registry['phpmd_out'] as $filename => $violations) {
if ($rows++ > 0) {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 4);
}
}
$ret_val .= dt('Filename: @filename, Violations: @total', array(
'@filename' => $filename,
'@total' => count($violations),
));
foreach ($violations as $violation) {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 6);
}
$begin = $violation['beginline'];
$end = $violation['endline'];
$rule = $violation['rule'];
$action = trim((string) $violation);
$ret_val .= "{$begin} to {$end} : {$rule} - {$action}";
}
}
}
return $ret_val;
}
/**
* Implements \SiteAudit\Check\Abstract\getAction().
*/
public function getAction() {
if (isset($this->registry['phpmd_path_error'])) {
return dt('Run "composer install" from the site_audit installation root to install missing dependencies.');
}
if (isset($this->registry['custom_code'])) {
return dt('Use the --custom-code option.');
}
if ($this
->getScore() == SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN) {
return dt('Fix the PHP Mess Detector violations.');
}
}
/**
* Implements \SiteAudit\Check\Abstract\calculateScore().
*/
public function calculateScore() {
// Get the path of phpmd.
$phpmd_path = $this
->getExecPath('phpmd');
if ($phpmd_path === '') {
$this->registry['phpmd_path_error'] = TRUE;
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
}
// Get the custom code paths.
// Get the custom code paths.
$custom_code = $this
->getCustomCodePaths();
if ($custom_code === FALSE) {
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}
if (empty($custom_code)) {
$this->registry['custom_code'] = TRUE;
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
}
// Get options.
$valid_options = array(
'minimumpriority' => NULL,
'suffixes' => '.php,.module,.install,.test,.inc,.profile,.theme',
'exclude' => '*.features.*,*_default.inc,*.ds.inc,*.strongarm.inc,*.panelizer.inc,*_defaults.inc,*.box.inc,*.context.inc,*displays.inc',
'strict' => NULL,
'ruleset' => 'codesize,naming,design,unusedcode',
);
$options = $this
->getOptions($valid_options, 'phpmd-');
$option_string = ' ' . $options['ruleset'];
foreach ($options as $option => $value) {
if ($option != 'ruleset') {
$option_string .= " --{$option}";
if ($value !== TRUE) {
$option_string .= "={$value}";
}
}
}
// Supress XML errors which will be handled by try catch instead.
libxml_use_internal_errors(TRUE);
foreach ($custom_code as $path) {
$command = $phpmd_path . ' ' . $path . ' xml' . $option_string;
$process = new Process($command);
$process
->run();
if ($process
->getExitCode() == 1) {
continue;
}
try {
$output = new SimpleXMLElement($process
->getOutput());
foreach ($output as $file) {
foreach ($file as $violation) {
$filename = $this
->getRelativePath((string) $file[0]['name']);
$this->registry['phpmd_out'][$filename][] = $violation;
}
}
} catch (Exception $e) {
$this
->logXmlError($path, 'phpmd');
continue;
}
}
if (empty($this->registry['phpmd_out'])) {
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
}
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SiteAuditCheckAbstract:: |
protected | property | Indicate that no other checks should be run after this check. | |
SiteAuditCheckAbstract:: |
private static | property | Use for checking whether custom code paths have been validated. | |
SiteAuditCheckAbstract:: |
protected | property | User has opted out of this check in configuration. | |
SiteAuditCheckAbstract:: |
protected | property | If set, will override the Report's percentage. | |
SiteAuditCheckAbstract:: |
protected | property | Use for passing data between checks within a report. | |
SiteAuditCheckAbstract:: |
protected | property | Quantifiable number associated with result on a scale of 0 to 2. | |
SiteAuditCheckAbstract:: |
constant | |||
SiteAuditCheckAbstract:: |
constant | |||
SiteAuditCheckAbstract:: |
constant | |||
SiteAuditCheckAbstract:: |
constant | |||
SiteAuditCheckAbstract:: |
public | function | Returns an array containing custom code paths or AUDIT_CHECK_SCORE_INFO. | |
SiteAuditCheckAbstract:: |
public | function | Returns the path of the executable. | |
SiteAuditCheckAbstract:: |
public | function | Returns the values of the valid options for a command. | |
SiteAuditCheckAbstract:: |
public | function | Get the report percent override, if any. | |
SiteAuditCheckAbstract:: |
public | function | Get the check registry. | |
SiteAuditCheckAbstract:: |
public | function | Gives path relative to DRUPAL_ROOT of the path is inside Drupal. | |
SiteAuditCheckAbstract:: |
public | function | Determine the result message based on the score. | |
SiteAuditCheckAbstract:: |
public | function | Get a quantifiable number representing a check result; lazy initialization. | |
SiteAuditCheckAbstract:: |
public | function | Get the CSS class associated with a score. | |
SiteAuditCheckAbstract:: |
public | function | Get the Drush message level associated with a score. | |
SiteAuditCheckAbstract:: |
public | function | Get a human readable label for a score. | |
SiteAuditCheckAbstract:: |
public | function | Logs error if unable to parse XML output. | |
SiteAuditCheckAbstract:: |
public | function | Display action items for a user to perform. | |
SiteAuditCheckAbstract:: |
public | function | Determine whether the check failed so badly that the report must stop. | |
SiteAuditCheckAbstract:: |
public | function | Constructor. | |
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\calculateScore(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getAction(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getDescription(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getLabel(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getResultFail(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getResultInfo(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getResultPass(). Overrides SiteAuditCheckAbstract:: |
|
SiteAuditCheckCodebasePhpMessDetection:: |
public | function |
Implements \SiteAudit\Check\Abstract\getResultWarn(). Overrides SiteAuditCheckAbstract:: |