Skip to content

Commit

Permalink
ConstructorAnalyzer - always find only non-abstract contructors (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Dec 2, 2021
1 parent 656c164 commit 93cf807
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .dev-tools/infection.json
Expand Up @@ -50,6 +50,7 @@
"Coalesce": true,
"ConcatOperandRemoval": true,
"Continue_": true,
"Decrement": true,
"DivEqual": true,
"Division": true,
"DoWhile": true,
Expand All @@ -65,6 +66,7 @@
"GreaterThanNegotiation": true,
"GreaterThanOrEqualToNegotiation": true,
"Identical": true,
"Increment": true,
"InstanceOf_": true,
"LessThanNegotiation": true,
"LessThanOrEqualToNegotiation": true,
Expand Down
2 changes: 0 additions & 2 deletions .dev-tools/src/InfectionConfigBuilder.php
Expand Up @@ -18,13 +18,11 @@ final class InfectionConfigBuilder
{
private const UNWANTED_MUTATORS = [
'Concat',
'Decrement',
'DecrementInteger',
'FalseValue',
'GreaterThan',
'GreaterThanOrEqualTo',
'IdenticalEqual',
'Increment',
'IncrementInteger',
'IntegerNegation',
'LessThan',
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3104-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3103-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
Expand Down
4 changes: 2 additions & 2 deletions src/Analyzer/ConstructorAnalyzer.php
Expand Up @@ -21,7 +21,7 @@
*/
final class ConstructorAnalyzer
{
public function findConstructor(Tokens $tokens, int $classIndex, bool $allowAbstract): ?ConstructorAnalysis
public function findNonAbstractConstructor(Tokens $tokens, int $classIndex): ?ConstructorAnalysis
{
if (!$tokens[$classIndex]->isGivenKind(\T_CLASS)) {
throw new \InvalidArgumentException(\sprintf('Index %d is not a class.', $classIndex));
Expand All @@ -39,7 +39,7 @@ public function findConstructor(Tokens $tokens, int $classIndex, bool $allowAbst
}

$constructorAttributes = $tokensAnalyzer->getMethodAttributes($index);
if (!$allowAbstract && $constructorAttributes['abstract']) {
if ($constructorAttributes['abstract']) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/ConstructorEmptyBracesFixer.php
Expand Up @@ -66,7 +66,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$constructorAnalysis = $constructorAnalyzer->findConstructor($tokens, $index, true);
$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index);
if ($constructorAnalysis === null) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/MultilinePromotedPropertiesFixer.php
Expand Up @@ -80,7 +80,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$constructorAnalysis = $constructorAnalyzer->findConstructor($tokens, $index, false);
$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index);
if ($constructorAnalysis === null) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PromotedConstructorPropertyFixer.php
Expand Up @@ -105,7 +105,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$constructorAnalysis = $constructorAnalyzer->findConstructor($tokens, $index, false);
$constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index);
if ($constructorAnalysis === null) {
continue;
}
Expand Down
24 changes: 5 additions & 19 deletions tests/Analyzer/ConstructorAnalyzerTest.php
Expand Up @@ -31,21 +31,21 @@ public function testFindingConstructorWhenNotForClass(): void
$tokens = Tokens::fromCode('<?php $no . $class . $here;');
$analyzer = new ConstructorAnalyzer();

$analyzer->findConstructor($tokens, 2, true);
$analyzer->findNonAbstractConstructor($tokens, 2);
}

/**
* @param array<int, null|int> $expected
*
* @dataProvider provideFindingNonAbstractConstructorCases
*/
public function testFindingNonAbstractConstructor(array $expected, bool $allowAbstract, string $code): void
public function testFindingNonAbstractConstructor(array $expected, string $code): void
{
$tokens = Tokens::fromCode($code);
$analyzer = new ConstructorAnalyzer();

foreach ($expected as $classIndex => $nonAbstractConstructorIndex) {
$constructorAnalysis = $analyzer->findConstructor($tokens, $classIndex, $allowAbstract);
$constructorAnalysis = $analyzer->findNonAbstractConstructor($tokens, $classIndex);

if ($nonAbstractConstructorIndex === null) {
self::assertNull($constructorAnalysis);
Expand All @@ -57,53 +57,40 @@ public function testFindingNonAbstractConstructor(array $expected, bool $allowAb
}

/**
* @return iterable<array{array<int, null|int>, bool, string}>
* @return iterable<array{array<int, null|int>, string}>
*/
public static function provideFindingNonAbstractConstructorCases(): iterable
{
yield 'no constructor' => [
[3 => null],
true,
'<?php abstract class Foo {
public function notConstructor() {}
}',
];

yield 'abstract constructor allowed to be found' => [
[3 => 13],
true,
'<?php abstract class Foo {
abstract public function __construct() {}
}',
];

yield 'abstract constructor not allowed to be found' => [
yield 'abstract constructor' => [
[3 => null],
false,
'<?php abstract class Foo {
abstract public function __construct() {}
}',
];

yield 'public constructor' => [
[3 => 11],
true,
'<?php abstract class Foo {
public function __construct() {}
}',
];

yield 'uppercase constructor' => [
[3 => 11],
true,
'<?php abstract class Foo {
public function __CONSTRUCT() {}
}',
];

yield 'class with other elements' => [
[3 => 29],
true,
'<?php abstract class Foo {
public $a;
public static function create() {}
Expand All @@ -114,7 +101,6 @@ public function bar() {}

yield 'multiple classes' => [
[2 => 10, 21 => null, 29 => 37],
true,
'<?php
class Foo {
public function __construct() {}
Expand Down

0 comments on commit 93cf807

Please sign in to comment.