Skip to content

Commit f58c739

Browse files
Update For compatibility (#717)
* Update composer.json * refactor: améliore la lisibilité et la robustesse du code en simplifiant les vérifications des interfaces et en ajoutant des contrôles de propriété * refactor: améliore la vérification des interfaces en évitant les appels redondants à class_implements --------- Co-authored-by: nuno maduro <enunomaduro@gmail.com>
1 parent 6b2d23f commit f58c739

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

src/Domain/Analyser.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ final class Analyser
4444
public function __call(string $method, array $args): mixed
4545
{
4646
$method = new ReflectionMethod(BaseAnalyser::class, $method);
47-
$method->setAccessible(true);
4847

4948
return $method->invoke(new BaseAnalyser(), ...$args);
5049
}
@@ -175,8 +174,10 @@ private function analyseFile(Collector $collector, string $filename): void
175174
$collector->incrementTraits();
176175
} elseif ($token === \T_INTERFACE) {
177176
$collector->incrementInterfaces();
178-
} elseif (isset($tokens[$i - 2]) &&
179-
\is_array($tokens[$i - 2])) {
177+
} elseif (
178+
isset($tokens[$i - 2]) &&
179+
\is_array($tokens[$i - 2])
180+
) {
180181
if ($tokens[$i - 2][0] === \T_ABSTRACT || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_ABSTRACT) {
181182
$collector->addAbstractClass($filename);
182183
} elseif ($tokens[$i - 2][0] === \T_FINAL || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_FINAL) {
@@ -205,8 +206,10 @@ private function analyseFile(Collector $collector, string $filename): void
205206
$next = $this->getNextNonWhitespaceTokenPos($tokens, $next);
206207
}
207208

208-
if (\is_array($tokens[$next]) &&
209-
$tokens[$next][0] === \T_STRING) {
209+
if (
210+
\is_array($tokens[$next]) &&
211+
$tokens[$next][0] === \T_STRING
212+
) {
210213
$functionName = $tokens[$next][1];
211214
} else {
212215
$currentBlock = 'anonymous function';
@@ -215,18 +218,22 @@ private function analyseFile(Collector $collector, string $filename): void
215218
}
216219

217220
if ($currentBlock === \T_FUNCTION) {
218-
if ($className === null &&
219-
$functionName !== 'anonymous function') {
221+
if (
222+
$className === null &&
223+
$functionName !== 'anonymous function'
224+
) {
220225
$collector->addNamedFunctions($functionName);
221226
} else {
222227
$static = false;
223228
$visibility = \T_PUBLIC;
224229

225230
for ($j = $i; $j > 0; $j--) {
226231
if (\is_string($tokens[$j])) {
227-
if ($tokens[$j] === '{' ||
232+
if (
233+
$tokens[$j] === '{' ||
228234
$tokens[$j] === '}' ||
229-
$tokens[$j] === ';') {
235+
$tokens[$j] === ';'
236+
) {
230237
break;
231238
}
232239

@@ -317,15 +324,19 @@ private function analyseFile(Collector $collector, string $filename): void
317324
break;
318325

319326
case \T_STRING:
320-
if ($value === 'define'
327+
if (
328+
$value === 'define'
321329
&& $tokens[$i - 1][1] !== '::'
322330
&& $tokens[$i - 1][1] !== '->'
323-
&& (! isset($tokens[$i - 2][1]) || $tokens[$i - 2][1] !== 'function')) {
331+
&& (! isset($tokens[$i - 2][1]) || $tokens[$i - 2][1] !== 'function')
332+
) {
324333
$j = $i + 1;
325334

326335
while (isset($tokens[$j]) && $tokens[$j] !== ';') {
327-
if (\is_array($tokens[$j]) &&
328-
$tokens[$j][0] === \T_CONSTANT_ENCAPSED_STRING) {
336+
if (
337+
\is_array($tokens[$j]) &&
338+
$tokens[$j][0] === \T_CONSTANT_ENCAPSED_STRING
339+
) {
329340
$collector->addGlobalConstant(\str_replace('\'', '', $tokens[$j][1]));
330341

331342
break;
@@ -344,18 +355,22 @@ private function analyseFile(Collector $collector, string $filename): void
344355
$n = $this->getNextNonWhitespaceTokenPos($tokens, $i);
345356
$nn = $this->getNextNonWhitespaceTokenPos($tokens, $n);
346357

347-
if ((bool) $n && (bool) $nn &&
358+
if (
359+
(bool) $n && (bool) $nn &&
348360
isset($tokens[$n][0]) &&
349361
($tokens[$n][0] === \T_STRING ||
350362
$tokens[$n][0] === \T_VARIABLE) &&
351-
$tokens[$nn] === '(') {
363+
$tokens[$nn] === '('
364+
) {
352365
if ($token === \T_DOUBLE_COLON) {
353366
$collector->incrementStaticMethodCalls();
354367
} else {
355368
$collector->incrementNonStaticMethodCalls();
356369
}
357-
} elseif ($token === \T_DOUBLE_COLON &&
358-
$tokens[$n][0] === \T_VARIABLE) {
370+
} elseif (
371+
$token === \T_DOUBLE_COLON &&
372+
$tokens[$n][0] === \T_VARIABLE
373+
) {
359374
$collector->incrementStaticAttributeAccesses();
360375
} elseif ($token === \T_OBJECT_OPERATOR) {
361376
$collector->incrementNonStaticAttributeAccesses();

src/Domain/Configuration.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,17 @@ private function resolveConfig(array $config): void
277277
$resolver->setDefined('timeout');
278278
$resolver->setAllowedValues(
279279
'preset',
280-
array_map(static fn (string $presetClass) => $presetClass::getName(), self::PRESETS)
280+
array_map(static fn(string $presetClass) => $presetClass::getName(), self::PRESETS)
281281
);
282282

283283
$resolver->setAllowedValues('add', $this->validateAddedInsight());
284284
$resolver->setAllowedValues('config', $this->validateConfigInsights());
285285
$resolver->setAllowedValues('requirements', $this->validateRequirements());
286286
$resolver->setAllowedTypes('threads', ['null', 'int']);
287287
$resolver->setAllowedTypes('diff_context', 'int');
288-
$resolver->setAllowedValues('diff_context', static fn ($value) => $value >= 0);
289-
$resolver->setAllowedValues('threads', static fn ($value) => $value === null || $value >= 1);
290-
$resolver->setAllowedValues('timeout', static fn ($value) => $value >= 0);
288+
$resolver->setAllowedValues('diff_context', static fn($value) => $value >= 0);
289+
$resolver->setAllowedValues('threads', static fn($value) => $value === null || $value >= 1);
290+
$resolver->setAllowedValues('timeout', static fn($value) => $value >= 0);
291291

292292
try {
293293
$config = $resolver->resolve($config);
@@ -328,10 +328,11 @@ private function validateAddedInsight(): Closure
328328
{
329329
return static function ($values): bool {
330330
foreach ($values as $metric => $insights) {
331+
$interfaces = class_implements($metric);
331332
if (
332333
! class_exists($metric) ||
333-
class_implements($metric) === false ||
334-
! in_array(Metric::class, class_implements($metric), true)
334+
!is_array($interfaces) ||
335+
! in_array(Metric::class, $interfaces, true)
335336
) {
336337
throw new InvalidConfiguration(sprintf(
337338
'Unable to use "%s" class as metric in section add.',

src/Domain/InsightLoader/FixerLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ final class FixerLoader implements InsightLoader
2020
{
2121
public function support(string $insightClass): bool
2222
{
23-
if (class_implements($insightClass) === false) {
23+
$interfaces = class_implements($insightClass);
24+
if (!is_array($interfaces)) {
2425
return false;
2526
}
26-
return array_key_exists(FixerInterface::class, class_implements($insightClass));
27+
return array_key_exists(FixerInterface::class, $interfaces);
2728
}
2829

2930
public function load(string $insightClass, string $dir, array $config, Collector $collector): Insight

src/Domain/InsightLoader/InsightLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ final class InsightLoader implements LoaderContract
1515
{
1616
public function support(string $insightClass): bool
1717
{
18-
if (class_implements($insightClass) === false) {
18+
$interfaces = class_implements($insightClass);
19+
if (!is_array($interfaces)) {
1920
return false;
2021
}
21-
return array_key_exists(Insight::class, class_implements($insightClass));
22+
return array_key_exists(Insight::class, $interfaces);
2223
}
2324

2425
public function load(string $insightClass, string $dir, array $config, Collector $collector): Insight

src/Domain/InsightLoader/SniffLoader.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ final class SniffLoader implements InsightLoader
1717
{
1818
public function support(string $insightClass): bool
1919
{
20-
if (class_implements($insightClass) === false) {
20+
$interfaces = class_implements($insightClass);
21+
if (!is_array($interfaces)) {
2122
return false;
2223
}
23-
return array_key_exists(SniffContract::class, class_implements($insightClass));
24+
return array_key_exists(SniffContract::class, $interfaces);
2425
}
2526

2627
public function load(string $insightClass, string $dir, array $config, Collector $collector): Insight
@@ -37,9 +38,11 @@ public function load(string $insightClass, string $dir, array $config, Collector
3738
}
3839

3940
foreach ($config as $property => $value) {
40-
$sniff->{$property} = $value;
41+
if (property_exists($sniff, $property)) {
42+
$sniff->{$property} = $value;
43+
}
4144
}
4245

4346
return new SniffDecorator($sniff, $dir, $excludeConfig);
4447
}
45-
}
48+
}

0 commit comments

Comments
 (0)