Skip to content

Commit

Permalink
Merge pull request #644 from TJVB/fixing_phpmd_errors
Browse files Browse the repository at this point in the history
Fixing a part of the PHPMD violations in PHPMD
  • Loading branch information
ravage84 committed Oct 20, 2019
2 parents f7e716f + 222ba30 commit 766349c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 42 deletions.
9 changes: 5 additions & 4 deletions src/main/php/PHPMD/ParserFactory.php
Expand Up @@ -95,11 +95,12 @@ private function init(Engine $pdepend, PHPMD $phpmd)
private function initInput(Engine $pdepend, PHPMD $phpmd)
{
foreach (explode(',', $phpmd->getInput()) as $path) {
if (is_dir(trim($path))) {
$pdepend->addDirectory(trim($path));
} else {
$pdepend->addFile(trim($path));
$trimmedPath = trim($path);
if (is_dir($trimmedPath)) {
$pdepend->addDirectory($trimmedPath);
continue;
}
$pdepend->addFile($trimmedPath);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/php/PHPMD/Rule/Naming/LongVariable.php
Expand Up @@ -57,16 +57,17 @@ public function apply(AbstractNode $node)
$this->checkNodeImage($declarator);
}
}
} else {
$declarators = $node->findChildrenOfType('VariableDeclarator');
foreach ($declarators as $declarator) {
$this->checkNodeImage($declarator);
}
$this->resetProcessed();
return;
}
$declarators = $node->findChildrenOfType('VariableDeclarator');
foreach ($declarators as $declarator) {
$this->checkNodeImage($declarator);
}

$variables = $node->findChildrenOfType('Variable');
foreach ($variables as $variable) {
$this->checkNodeImage($variable);
}
$variables = $node->findChildrenOfType('Variable');
foreach ($variables as $variable) {
$this->checkNodeImage($variable);
}

$this->resetProcessed();
Expand Down
53 changes: 40 additions & 13 deletions src/main/php/PHPMD/Rule/Naming/ShortVariable.php
Expand Up @@ -50,25 +50,52 @@ public function apply(AbstractNode $node)
$this->resetProcessed();

if ($node->getType() === 'class') {
$fields = $node->findChildrenOfType('FieldDeclaration');
foreach ($fields as $field) {
$declarators = $field->findChildrenOfType('VariableDeclarator');
foreach ($declarators as $declarator) {
$this->checkNodeImage($declarator);
}
}
} else {
$declarators = $node->findChildrenOfType('VariableDeclarator');
$this->applyClass($node);
return;
}

$this->applyNoClass($node);
}

/**
* Extracts all variable and variable declarator nodes from the given class node
* and checks the variable name length against the configured minimum
* length.
*
* @param AbstractNode $node
* @return void
*/
private function applyClass(AbstractNode $node)
{
$fields = $node->findChildrenOfType('FieldDeclaration');
foreach ($fields as $field) {
$declarators = $field->findChildrenOfType('VariableDeclarator');
foreach ($declarators as $declarator) {
$this->checkNodeImage($declarator);
}
}
$this->resetProcessed();
}

$variables = $node->findChildrenOfType('Variable');
foreach ($variables as $variable) {
$this->checkNodeImage($variable);
}
/**
* Extracts all variable and variable declarator nodes from the given no class node
* and checks the variable name length against the configured minimum
* length.
*
* @param AbstractNode $node
* @return void
*/
private function applyNoClass(AbstractNode $node)
{
$declarators = $node->findChildrenOfType('VariableDeclarator');
foreach ($declarators as $declarator) {
$this->checkNodeImage($declarator);
}

$variables = $node->findChildrenOfType('Variable');
foreach ($variables as $variable) {
$this->checkNodeImage($variable);
}
$this->resetProcessed();
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/php/PHPMD/RuleSetFactory.php
Expand Up @@ -61,9 +61,9 @@ public function __construct()
// PEAR installer workaround
if (strpos($this->location, '@data_dir') === 0) {
$this->location = __DIR__ . '/../../resources';
} else {
$this->location .= '/PHPMD/resources';
return;
}
$this->location .= '/PHPMD/resources';
}

/**
Expand Down Expand Up @@ -248,11 +248,13 @@ private function parseRuleNode(RuleSet $ruleSet, \SimpleXMLElement $node)
{
if (substr($node['ref'], -3, 3) === 'xml') {
$this->parseRuleSetReferenceNode($ruleSet, $node);
} elseif ('' === (string) $node['ref']) {
return;
}
if ('' === (string) $node['ref']) {
$this->parseSingleRuleNode($ruleSet, $node);
} else {
$this->parseRuleReferenceNode($ruleSet, $node);
return;
}
$this->parseRuleReferenceNode($ruleSet, $node);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Expand Up @@ -27,6 +27,8 @@
/**
* This is a helper class that collects the specified cli arguments and puts them
* into accessible properties.
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
class CommandLineOptions
{
Expand Down
22 changes: 11 additions & 11 deletions src/main/php/PHPMD/Writer/StreamWriter.php
Expand Up @@ -40,18 +40,18 @@ public function __construct($streamResourceOrUri)
{
if (is_resource($streamResourceOrUri) === true) {
$this->stream = $streamResourceOrUri;
} else {
$dirName = dirname($streamResourceOrUri);
if (file_exists($dirName) === false) {
mkdir($dirName, 0777, true);
}
if (file_exists($dirName) === false) {
$message = 'Cannot find output directory "' . $dirName . '".';
throw new \RuntimeException($message);
}

$this->stream = fopen($streamResourceOrUri, 'wb');
return;
}
$dirName = dirname($streamResourceOrUri);
if (file_exists($dirName) === false) {
mkdir($dirName, 0777, true);
}
if (file_exists($dirName) === false) {
$message = 'Cannot find output directory "' . $dirName . '".';
throw new \RuntimeException($message);
}

$this->stream = fopen($streamResourceOrUri, 'wb');
}

/**
Expand Down

0 comments on commit 766349c

Please sign in to comment.