Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #765 from frankdekker/class-name-length-rule
Class name length rule
- Loading branch information
Showing
23 changed files
with
967 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Rule\Naming; | ||
|
||
use PHPMD\AbstractNode; | ||
use PHPMD\AbstractRule; | ||
use PHPMD\Rule\ClassAware; | ||
use PHPMD\Rule\InterfaceAware; | ||
use PHPMD\Utility\Strings; | ||
|
||
/** | ||
* This rule checks if an interface or class name exceeds the configured length excluding certain configured suffixes | ||
*/ | ||
class LongClassName extends AbstractRule implements ClassAware, InterfaceAware | ||
{ | ||
/** | ||
* Temporary cache of configured suffixes to subtract | ||
* | ||
* @var string[]|null | ||
*/ | ||
private $subtractSuffixes; | ||
|
||
/** | ||
* Check if a class name exceeds the configured maximum length and emit a rule violation | ||
* | ||
* @param \PHPMD\AbstractNode $node | ||
* @return void | ||
*/ | ||
public function apply(AbstractNode $node) | ||
{ | ||
$threshold = $this->getIntProperty('maximum'); | ||
$classOrInterfaceName = $node->getName(); | ||
if (Strings::lengthWithoutSuffixes($classOrInterfaceName, $this->getSubtractSuffixList()) <= $threshold) { | ||
return; | ||
} | ||
$this->addViolation($node, array($classOrInterfaceName, $threshold)); | ||
} | ||
|
||
/** | ||
* Gets array of suffixes from property | ||
* | ||
* @return string[] | ||
*/ | ||
private function getSubtractSuffixList() | ||
{ | ||
if ($this->subtractSuffixes === null) { | ||
$this->subtractSuffixes = Strings::splitToList( | ||
$this->getStringProperty('subtract-suffixes', ''), | ||
',' | ||
); | ||
} | ||
|
||
return $this->subtractSuffixes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Rule\Naming; | ||
|
||
use PHPMD\AbstractNode; | ||
use PHPMD\AbstractRule; | ||
use PHPMD\Rule\ClassAware; | ||
use PHPMD\Rule\InterfaceAware; | ||
use PHPMD\Utility\Strings; | ||
|
||
/** | ||
* This rule will detect classes and interfaces with names that are too short. | ||
*/ | ||
class ShortClassName extends AbstractRule implements ClassAware, InterfaceAware | ||
{ | ||
/** | ||
* Temporary cache of configured exceptions. Have name as key | ||
* | ||
* @var array<string, int>|null | ||
*/ | ||
private $exceptions; | ||
|
||
/** | ||
* Check if a class or interface name is below the minimum configured length and emit a rule violation | ||
* | ||
* @param \PHPMD\AbstractNode $node | ||
* @return void | ||
*/ | ||
public function apply(AbstractNode $node) | ||
{ | ||
$threshold = $this->getIntProperty('minimum'); | ||
$classOrInterfaceName = $node->getName(); | ||
if (strlen($classOrInterfaceName) >= $threshold) { | ||
return; | ||
} | ||
|
||
$exceptions = $this->getExceptionsList(); | ||
if (isset($exceptions[$classOrInterfaceName])) { | ||
return; | ||
} | ||
|
||
$this->addViolation($node, array($classOrInterfaceName, $threshold)); | ||
} | ||
|
||
/** | ||
* Gets array of exceptions from property | ||
* | ||
* @return array<string, int> | ||
*/ | ||
private function getExceptionsList() | ||
{ | ||
if ($this->exceptions === null) { | ||
$this->exceptions = array_flip( | ||
Strings::splitToList($this->getStringProperty('exceptions', ''), ',') | ||
); | ||
} | ||
|
||
return $this->exceptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Utility; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Utility class to provide string checks and manipulations | ||
*/ | ||
class Strings | ||
{ | ||
/** | ||
* Returns the length of the given string, excluding at most one suffix | ||
* | ||
* @param string $stringName String to calculate the length for. | ||
* @param array $subtractSuffixes List of suffixes to exclude from the calculated length. | ||
* @return int The length of the string, without suffix, if applicable. | ||
*/ | ||
public static function lengthWithoutSuffixes($stringName, array $subtractSuffixes) | ||
{ | ||
$stringLength = strlen($stringName); | ||
|
||
foreach ($subtractSuffixes as $suffix) { | ||
$suffixLength = strlen($suffix); | ||
if (substr($stringName, -$suffixLength) === $suffix) { | ||
$stringLength -= $suffixLength; | ||
break; | ||
} | ||
} | ||
|
||
return $stringLength; | ||
} | ||
|
||
/** | ||
* Split a string with the given separator, trim whitespaces around the parts and remove any empty strings | ||
* | ||
* @param string $listAsString The string to split. | ||
* @param string $separator The separator to split the string with, similar to explode. | ||
* @return array The list of trimmed and filtered parts of the string. | ||
* @throws InvalidArgumentException When the separator is an empty string. | ||
*/ | ||
public static function splitToList($listAsString, $separator) | ||
{ | ||
if ($separator === '') { | ||
throw new InvalidArgumentException("Separator can't be empty string"); | ||
} | ||
|
||
return array_filter( | ||
array_map('trim', explode($separator, $listAsString)), | ||
function ($value) { | ||
return $value !== ''; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.