1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP Mess Detector. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Licensed under BSD License |
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
11
|
|
|
* |
12
|
|
|
* @author Manuel Pichler <[email protected]> |
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
15
|
|
|
* @link http://phpmd.org/ |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace PHPMD\Rule\Naming; |
19
|
|
|
|
20
|
|
|
use PHPMD\AbstractNode; |
21
|
|
|
use PHPMD\AbstractRule; |
22
|
|
|
use PHPMD\Rule\ClassAware; |
23
|
|
|
use PHPMD\Rule\InterfaceAware; |
24
|
|
|
use PHPMD\Utility\Strings; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This rule will detect classes and interfaces with names that are too short. |
28
|
|
|
*/ |
29
|
|
|
class ShortClassName extends AbstractRule implements ClassAware, InterfaceAware |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Temporary cache of configured exceptions. Have name as key |
33
|
|
|
* |
34
|
|
|
* @var array<string, int>|null |
35
|
|
|
*/ |
36
|
|
|
private $exceptions; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check if a class or interface name is below the minimum configured length and emit a rule violation |
40
|
|
|
* |
41
|
|
|
* @param \PHPMD\AbstractNode $node |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function apply(AbstractNode $node) |
45
|
|
|
{ |
46
|
|
|
$threshold = $this->getIntProperty('minimum'); |
47
|
|
|
$classOrInterfaceName = $node->getName(); |
48
|
|
|
if (strlen($classOrInterfaceName) >= $threshold) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$exceptions = $this->getExceptionsList(); |
53
|
|
|
if (isset($exceptions[$classOrInterfaceName])) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->addViolation($node, array($classOrInterfaceName, $threshold)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets array of exceptions from property |
62
|
|
|
* |
63
|
|
|
* @return array<string, int> |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
private function getExceptionsList() |
66
|
|
|
{ |
67
|
|
|
if ($this->exceptions === null) { |
68
|
|
|
$this->exceptions = array_flip( |
69
|
|
|
Strings::splitToList($this->getStringProperty('exceptions', ''), ',') |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->exceptions; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.