8000 Add ExceptionProperty util to handle pattern in exceptions property · phpmd/phpmd@e40380d · GitHub
[go: up one dir, main page]

Skip to content

Commit e40380d

Browse files
committed
Add ExceptionProperty util to handle pattern in exceptions property
1 parent 9ba3709 commit e40380d

File tree

12 files changed

+348
-66
lines changed

12 files changed

+348
-66
lines changed

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
/**
@@ -80,19 +81,21 @@ protected function isBooleanValue(ASTValue $value = null)
8081
}
8182

8283
/**
84+
* @deprecated getExceptionsList() is not consistent across rules,
85+
* you can use ExceptionProperty::fromRule($rule)->flip() instead.
86+
* In the next major version, ExceptionProperty will be used to provide
87+
* an uniformized set of tools to handle the list of exceptions for a rule.
88+
*
89+
* @codeCoverageIgnore
90+
*
8391
* Gets exceptions from property
8492
*
8593
* @return array<string, int>
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)
@@ -103,23 +104,19 @@ protected function isMethodIgnored(AbstractNode $methodCall, $ignorePattern)
103104
}
104105

105106
/**
107+
* @deprecated getExceptionsList() is not consistent across rules,
108+
* you can use ExceptionProperty::fromRule($rule)->trim() instead.
109+
* In the next major version, ExceptionProperty will be used to provide
110+
* an uniformized set of tools to handle the list of exceptions for a rule.
111+
*
112+
* @codeCoverageIgnore
113+
*
106114
* Gets array of exceptions from property
107115
*
108116
* @return array
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
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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 long names.
@@ -40,13 +41,7 @@ public function apply(AbstractNode $node)
4041
$threshold = $this->getIntProperty('maximum');
4142
$name = (string)$node->getName();
4243

43-
if (strlen($name) <= $threshold) {
44-
return;
45-
}
46-
47-
$exceptions = $this->getExceptionsList();
48-
49-
if (in_array($name, $exceptions, true)) {
44+
if (strlen($name) <= $threshold || ExceptionProperty::fromRule($this)->matches($name)) {
5045
return;
5146
}
5247

@@ -61,18 +56,19 @@ public function apply(AbstractNode $node)
6156
}
6257

6358
/**
59+
* @deprecated getExceptionsList() is not consistent across rules,
60+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
61+
* In the next major version, ExceptionProperty will be used to provide
62+
* an uniformized set of tools to handle the list of exceptions for a rule.
63+
*
64+
* @codeCoverageIgnore
65+
*
6466
* Gets array of exceptions from property
6567
*
6668
* @return array
6769
*/
6870
protected function getExceptionsList()
6971
{
70-
try {
71-
$exceptions = $this->getStringProperty('exceptions');
72-
} catch (\OutOfBoundsException $e) {
73-
$exceptions = '';
74-
}
75-
76-
return explode(',', $exceptions);
72+
return ExceptionProperty::fromRule($this)->toArray();
7773
}
7874
}

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.
@@ -60,16 +60,21 @@ public function apply(AbstractNode $node)
6060
}
6161

6262
/**
63+
* @deprecated getExceptionsList() is not consistent across rules,
64+
* you can use ExceptionProperty::fromRule($rule)->flip() instead.
65+
* In the next major version, ExceptionProperty will be used to provide
66+
* an uniformized set of tools to handle the list of exceptions for a rule.
67+
*
68+
* @codeCoverageIgnore
69+
*
6370
* Gets array of exceptions from property
6471
*
6572
* @return array<string, int>
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: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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.
@@ -40,13 +41,7 @@ public function apply(AbstractNode $node)
4041
$threshold = $this->getIntProperty('minimum');
4142
$name = (string)$node->getName();
4243

43-
if (strlen($name) >= $threshold) {
44-
return;
45-
}
46-
47-
$exceptions = $this->getExceptionsList();
48-
49-
if (in_array($name, $exceptions, true)) {
44+
if (strlen($name) >= $threshold || ExceptionProperty::fromRule($this)->matches($name)) {
5045
return;
5146
}
5247

@@ -61,18 +56,19 @@ public function apply(AbstractNode $node)
6156
}
6257

6358
/**
59+
* @deprecated getExceptionsList() is not consistent across rules,
60+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
61+
* In the next major version, ExceptionProperty will be used to provide
62+
* an uniformized set of tools to handle the list of exceptions for a rule.
63+
*
64+
* @codeCoverageIgnore
65+
*
6466
* Gets array of exceptions from property
6567
*
6668
* @return array
6769
*/
6870
protected function getExceptionsList()
6971
{
70-
try {
71-
$exceptions = $this->getStringProperty('exceptions');
72-
} catch (\OutOfBoundsException $e) {
73-
$exceptions = '';
74-
}
75-
76-
return explode(',', $exceptions);
72+
return ExceptionProperty::fromRule($this)->toArray();
7773
}
7874
}

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,29 +138,28 @@ 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

146145
$this->addViolation($node, array($node->getImage(), $threshold));
147146
}
148147

149148
/**
149+
* @deprecated getExceptionsList() is not consistent across rules,
150+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
151+
* In the next major version, ExceptionProperty will be used to provide
152+
* an uniformized set of tools to handle the list of exceptions for a rule.
153+
*
154+
* @codeCoverageIgnore
155+
*
150156
* Gets array of exceptions from property
151157
*
152158
* @return array
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
@@ -287,12 +288,19 @@ protected function isChildOf(AbstractNode $node, $type)
287288
}
288289

289290
/**
291+
* @deprecated getExceptionsList() is not consistent across rules,
292+
* you can use ExceptionProperty::fromRule($rule)->toArray() instead.
293+
* In the next major version, ExceptionProperty will be used to provide
294+
* an uniformized set of tools to handle the list of exceptions for a rule.
295+
*
296+
* @codeCoverageIgnore
297+
*
290298
* Gets array of exceptions from property
291299
*
292300
* @return array
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