Skip to content

Commit

Permalink
Fix Should rules ignoring classes with empty findings
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosas authored Aug 25, 2023
1 parent 5abcac7 commit 5ed4a75
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Rule/Assertion/Relation/RelationAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPat\Configuration;
use PHPat\Rule\Assertion\Assertion;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend;
use PHPat\Rule\Assertion\Relation\ShouldImplement\ShouldImplement;
use PHPat\Selector\Classname;
use PHPat\Selector\SelectorInterface;
use PHPat\ShouldNotHappenException;
Expand Down Expand Up @@ -95,6 +97,11 @@ protected function ruleApplies(Scope $scope, array $nodes): bool
return false;
}

// Can not skip if the rule is a ShouldExtend or ShouldImplement rule
if (is_a($this, ShouldExtend::class) || is_a($this, ShouldImplement::class)) {
return true;
}

if (empty($nodes)) {
return false;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/rules/ShouldExtend/SimpleParentClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldExtend;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ParentClassRule;
use PHPat\Rule\Assertion\Relation\ShouldExtend\ShouldExtend;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\Simple\SimpleAbstractClass;
use Tests\PHPat\fixtures\Simple\SimpleClass;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<ParentClassRule>
* @internal
* @coversNothing
*/
class SimpleParentClassTest extends RuleTestCase
{
public const RULE_NAME = 'test_SimpleClassShouldExtendSimpleAbstractClass';

public function testRule(): void
{
$this->analyse(['tests/fixtures/Simple/SimpleClass.php'], [
[sprintf('%s should extend %s', SimpleClass::class, SimpleAbstractClass::class), 5],
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldExtend::class,
[new Classname(SimpleClass::class, false)],
[new Classname(SimpleAbstractClass::class, false)]
);

return new ParentClassRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
$this->createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\PHPat\unit\rules\ShouldImplement;

use PHPat\Configuration;
use PHPat\Rule\Assertion\Relation\ShouldImplement\ImplementedInterfacesRule;
use PHPat\Rule\Assertion\Relation\ShouldImplement\ShouldImplement;
use PHPat\Selector\Classname;
use PHPat\Statement\Builder\StatementBuilderFactory;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\FileTypeMapper;
use Tests\PHPat\fixtures\Simple\SimpleClass;
use Tests\PHPat\fixtures\Simple\SimpleInterface;
use Tests\PHPat\unit\FakeTestParser;

/**
* @extends RuleTestCase<ImplementedInterfacesRule>
* @internal
* @coversNothing
*/
class SimpleImplementedInterfacesTest extends RuleTestCase
{
public const RULE_NAME = 'test_SimpleClassShouldImplementSimpleInterface';

public function testRule(): void
{
$this->analyse(['tests/fixtures/Simple/SimpleClass.php'], [
[sprintf('%s should implement %s', SimpleClass::class, SimpleInterface::class), 5],
]);
}

protected function getRule(): Rule
{
$testParser = FakeTestParser::create(
self::RULE_NAME,
ShouldImplement::class,
[new Classname(SimpleClass::class, false)],
[new Classname(SimpleInterface::class, false)]
);

return new ImplementedInterfacesRule(
new StatementBuilderFactory($testParser),
new Configuration(false, true, false),
$this->createReflectionProvider(),
self::getContainer()->getByType(FileTypeMapper::class)
);
}
}

0 comments on commit 5ed4a75

Please sign in to comment.