8000 Implement LongMethodName · phpmd/phpmd@7610a25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7610a25

Browse files
committed
Implement LongMethodName
Fix #486
1 parent 59fa602 commit 7610a25

32 files changed

+827
-64
lines changed

src/main/php/PHPMD/AbstractNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function getImage()
200200
* Returns the source name for this node, maybe a class or interface name,
201201
* or a package, method, function name.
202202
*
203-
* @return string
203+
* @return string|null
204204
*/
205205
public function getName()
206206
{

src/main/php/PHPMD/Rule/CleanCode/BooleanArgumentFlag.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPMD\AbstractRule;
2424
use PHPMD\Rule\FunctionAware;
2525
use PHPMD\Rule\MethodAware;
26+
use PHPMD\Utility\ExceptionProperty;
2627
use PHPMD\Utility\Strings;
2728

2829
/**
@@ -83,16 +84,18 @@ protected function isBooleanValue(ASTValue $value = null)
8384
* Gets exceptions from property
8485
*
8586
* @return array<string, int>
87+
*
88+
* @codeCoverageIgnore
89+
*
90+
* @deprecated getExceptionsList() is not consistent across rules,
91+
* you can use ExceptionProperty::fromRule($rule)->flip() instead.
92+
* In the next major version, ExceptionProperty will be used to provide
93+
* an uniformized set of tools to handle the list of exceptions for a rule.
8694
*/
8795
protected function getExceptionsList()
8896
{
8997
if ($this->exceptions === null) {
90-
$this->exceptions = array_flip(
91-
Strings::splitToList(
92-
$this->getStringProperty('exceptions', ''),
93-
','
94-
)
95-
);
98+
$this->exceptions = ExceptionProperty::fromRule($this)->flip();
9699
}
97100

98101
return $this->exceptions;

src/main/php/PHPMD/Rule/CleanCode/StaticAccess.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPMD\AbstractRule;
2626
use PHPMD\Rule\FunctionAware;
2727
use PHPMD\Rule\MethodAware;
28+
use PHPMD\Utility\ExceptionProperty;
2829

2930
/**
3031
* Check if static access is used in a method.
@@ -66,7 +67,7 @@ public function apply(AbstractNode $node)
6667

6768
protected function isExcludedFromAnalysis($className, $exceptions)
6869
{
69-
return in_array(trim($className, " \t\n\r\0\x0B\\"), $exceptions);
70+
return in_array(trim($className, " \t\n\r\0\x0B\\"), $exceptions, true);
7071
}
7172

7273
protected function isStaticMethodCall(AbstractNode $methodCall)
@@ -106,20 +107,16 @@ protected function isMethodIgnored(AbstractNode $methodCall, $ignorePattern)
106107
* Gets array of exceptions from property
107108
*
108109
* @return array
110+
*
111+
* @codeCoverageIgnore
112+
*
113+
* @deprecated getExceptionsList() is not consistent across rules,
114+
* you can use ExceptionProperty::fromRule($rule)->trim() instead.
115+
* In the next major version, ExceptionProperty will be used to provide
116+
* an uniformized set of tools to handle the list of exceptions for a rule.
109117
*/
110118
protected function getExceptionsList()
111119
{
112-
try {
113-
$exceptions = $this->getStringProperty('exceptions');
114-
} catch (\OutOfBoundsException $e) {
115-
$exceptions = '';
116-
}
117-
118-
return array_map(
119-
function ($className) {
120-
return trim($className, " \t\n\r\0\x0B\\");
121-
},
122-
explode(',', $exceptions)
123-
);
120+
return ExceptionProperty::fromRule($this)->trim();
124121
}
125122
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* This file is part of PHP Mess Detector.
4+
*
5+
* Copyright (c) Manuel Pichler <mapi@phpmd.org>.
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 <mapi@phpmd.org>
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\FunctionAware;
23+
use PHPMD\Rule\MethodAware;
24+
use PHPMD\Utility\ExceptionProperty;
25+
26+
/**
27+
* This rule class will detect methods and functions with very long names.
28+
*/
29+
class LongMethodName extends AbstractRule implements MethodAware, FunctionAware
30+
{
31+
/**
32+
* Extracts all method and function nodes from the given node and
33+
* checks the name length against the configured maximum length.
34+
*
35+
* @param \PHPMD\AbstractNode $node
36+
* @return void
37+
*/
38+
public function apply(AbstractNode $node)
39+
{
40+
$threshold = $this->getIntProperty('maximum');
41+
$name = (string)$node->getName();
42+
43+
if (strlen($name) <= $threshold || ExceptionProperty::fromRule($this)->matches($name)) {
44+
return;
45+
}
46+
47+
$this->addViolation(
48+
$node,
49+
array(
50+
$node->getParentName(),
51+
$node->getName(),
52+
$threshold,
53+
)
54+
);
55+
}
56+
57+
/**
58+
* Gets array of exceptions from property
59+
*
60+
* @return array
61+
*
62+
* @codeCoverageIgnore
63+
*
64+
* @deprecated getExceptionsList() is not consistent across rules,
65+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
66+
* In the next major version, ExceptionProperty will be used to provide
67+
* an uniformized set of tools to handle the list of exceptions for a rule.
6 10000 8+
*/
69+
protected function getExceptionsList()
70+
{
71+
return ExceptionProperty::fromRule($this)->toArray();
72+
}
73+
}

src/main/php/PHPMD/Rule/Naming/ShortClassName.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PHPMD\Rule\EnumAware;
2424
use PHPMD\Rule\InterfaceAware;
2525
use PHPMD\Rule\TraitAware;
26-
use PHPMD\Utility\Strings;
26+
use PHPMD\Utility\ExceptionProperty;
2727

2828
/**
2929
* This rule will detect classes and interfaces with names that are too short.
@@ -63,13 +63,18 @@ public function apply(AbstractNode $node)
6363
* Gets array of exceptions from property
6464
*
6565
* @return array<string, int>
66+
*
67+
* @codeCoverageIgnore
68+
*
69+
* @deprecated getExceptionsList() is not consistent across rules,
70+
* you can use ExceptionProperty::fromRule($rule)->flip() instead.
71+
* In the next major version, ExceptionProperty will be used to provide
72+
* an uniformized set of tools to handle the list of exceptions for a rule.
6673
*/
6774
protected function getExceptionsList()
6875
{
6976
if ($this->exceptions === null) {
70-
$this->exceptions = array_flip(
71-
Strings::splitToList($this->getStringProperty('exceptions', ''), ',')
72-
);
77+
$this->exceptions = ExceptionProperty::fromRule($this)->flip();
7378
}
7479

7580
return $this->exceptions;

src/main/php/PHPMD/Rule/Naming/ShortMethodName.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,26 @@
2121
use PHPMD\AbstractRule;
2222
use PHPMD\Rule\FunctionAware;
2323
use PHPMD\Rule\MethodAware;
24+
use PHPMD\Utility\ExceptionProperty;
2425

2526
/**
2627
* This rule class will detect methods and functions with very short names.
2728
*/
2829
class ShortMethodName extends AbstractRule implements MethodAware, FunctionAware
2930
{
3031
/**
31-
* Extracts all variable and variable declarator nodes from the given node
32-
* and checks the variable name length against the configured minimum
33-
* length.
32+
* Extracts all method and function nodes from the given node
33+
* and checks the name length against the configured minimum length.
3434
*
3535
* @param \PHPMD\AbstractNode $node
3636
* @return void
3737
*/
3838
public function apply(AbstractNode $node)
3939
{
4040
$threshold = $this->getIntProperty('minimum');
41-
if ($threshold <= strlen($node->getName())) {
42-
return;
43-
}
41+
$name = (string)$node->getName();
4442

45-
$exceptions = $this->getExceptionsList();
46-
47-
if (in_array($node->getName(), $exceptions)) {
43+
if (strlen($name) >= $threshold || ExceptionProperty::fromRule($this)->matches($name)) {
4844
return;
4945
}
5046

@@ -62,15 +58,16 @@ public function apply(AbstractNode $node)
6258
* Gets array of exceptions from property
6359
*
6460
* @return array
61+
*
62+
* @codeCoverageIgnore
63+
*
64+
* @deprecated getExceptionsList() is not consistent across rules,
65+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
66+
* In the next major version, ExceptionProperty will be used to provide
67+
* an uniformized set of tools to handle the list of exceptions for a rule.
6568
*/
6669
protected function getExceptionsList()
6770
{
68-
try {
69-
$exceptions = $this->getStringProperty('exceptions');
70-
} catch (\OutOfBoundsException $e) {
71-
$exceptions = '';
72-
}
73-
74-
return explode(',', $exceptions);
71+
return ExceptionProperty::fromRule($this)->toArray();
7572
}
7673
}

src/main/php/PHPMD/Rule/Naming/ShortVariable.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPMD\Rule\FunctionAware;
2424
use PHPMD\Rule\MethodAware;
2525
use PHPMD\Rule\TraitAware;
26+
use PHPMD\Utility\ExceptionProperty;
2627

2728
/**
2829
* This rule class will detect variables, parameters and properties with short
@@ -137,9 +138,7 @@ protected function checkMinimumLength(AbstractNode $node)
137138
return;
138139
}
139140

140-
$exceptions = $this->getExceptionsList();
141-
142-
if (in_array(substr($node->getImage(), 1), $exceptions)) {
141+
if (ExceptionProperty::fromRule($this)->contains(substr($node->getImage(), 1))) {
143142
return;
144143
}
145144

@@ -150,16 +149,17 @@ protected function checkMinimumLength(AbstractNode $node)
150149
* Gets array of exceptions from property
151150
*
152151
* @return array
152+
*
153+
* @codeCoverageIgnore
154+
*
155+
* @deprecated getExceptionsList() is not consistent across rules,
156+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
157+
* In the next major version, ExceptionProperty will be used to provide
158+
* an uniformized set of tools to handle the list of exceptions for a rule.
153159
*/
154160
protected function getExceptionsList()
155161
{
156-
try {
157-
$exceptions = $this->getStringProperty('exceptions');
158-
} catch (\OutOfBoundsException $e) {
159-
$exceptions = '';
160-
}
161-
162-
return explode(',', $exceptions);
162+
return ExceptionProperty::fromRule($this)->toArray();
163163
}
164164

165165
/**

src/main/php/PHPMD/Rule/UnusedLocalVariable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPMD\AbstractNode;
2121
use PHPMD\Node\AbstractCallableNode;
2222
use PHPMD\Node\ASTNode;
23+
use PHPMD\Utility\ExceptionProperty;
2324

2425
/**
2526
* This rule collects all local variables within a given function or method
@@ -290,9 +291,16 @@ protected function isChildOf(AbstractNode $node, $type)
290291
* Gets array of exceptions from property
291292
*
292293
* @return array
294+
*
295+
* @codeCoverageIgnore
296+
*
297+
* @deprecated getExceptionsList() is not consistent across rules,
298+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
299+
* In the next major version, ExceptionProperty will be used to provide
300+
* an uniformized set of tools to handle the list of exceptions for a rule.
293301
*/
294302
protected function getExceptionsList()
295303
{
296-
return explode(',', $this->getStringProperty('exceptions', ''));
304+
return ExceptionProperty::fromRule($this)->toArray();
297305
}
298306
}

0 commit comments

Comments
 (0)
0