Skip to content

Commit

Permalink
Consider config for NarrowReturnObjectTypeRule (#13)
Browse files Browse the repository at this point in the history
* fix: Consider if configuration  is active for NarrowReturnObjectTypeRule

Fixes #12

* chore: Fix code style

* fix: Use correct command to check the code style
  • Loading branch information
mitelg committed Jun 17, 2024
1 parent 5d4f437 commit 06a5fb5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

-
name: 'Coding Standard'
run: composer fix-cs --ansi
run: composer check-cs --ansi

-
name: 'Tests'
Expand Down
8 changes: 4 additions & 4 deletions src/Collector/MethodCall/MethodCallArgTypesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Rector\TypePerfect\Collector\MethodCall;

use PhpParser\Node\Identifier;
use PHPStan\Reflection\ExtendedMethodReflection;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ExtendedMethodReflection;
use Rector\TypePerfect\Matcher\ClassMethodCallReferenceResolver;
use Rector\TypePerfect\Printer\CollectorMetadataPrinter;
use Rector\TypePerfect\ValueObject\MethodCallReference;
Expand All @@ -36,13 +36,13 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): ?array
{
if ($node->isFirstClassCallable() || $node->getArgs() === [] || !$node->name instanceof Identifier) {
if ($node->isFirstClassCallable() || $node->getArgs() === [] || ! $node->name instanceof Identifier) {
return null;
}

$methodCalledOnType = $scope->getType($node->var);
$methodReflection = $scope->getMethodReflection($methodCalledOnType, $node->name->name);
if (!$methodReflection instanceof ExtendedMethodReflection) {
if (! $methodReflection instanceof ExtendedMethodReflection) {
return null;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Rules/NarrowReturnObjectTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\TypePerfect\Configuration;
use Rector\TypePerfect\NodeFinder\ReturnNodeFinder;
use Rector\TypePerfect\Reflection\MethodNodeAnalyser;

/**
* @see \Rector\TypePerfect\Tests\Rules\RequireSpecificReturnTypeOverAbstractRule\RequireSpecificReturnTypeOverAbstractRuleTest
* @see \Rector\TypePerfect\Tests\Rules\NarrowReturnObjectTypeRule\NarrowReturnObjectTypeRuleTest
*
* @implements Rule<ClassMethod>
*/
Expand All @@ -32,6 +33,7 @@
public function __construct(
private ReturnNodeFinder $returnNodeFinder,
private MethodNodeAnalyser $methodNodeAnalyser,
private Configuration $configuration
) {
}

Expand All @@ -49,6 +51,10 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $this->configuration->isNarrowEnabled()) {
return [];
}

if (! $node->returnType instanceof FullyQualified) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function provideData(): Iterator

$argErrorMessage = sprintf(NarrowPublicClassMethodParamTypeRule::ERROR_MESSAGE, 'int');
yield [[
__DIR__ . '/Fixture/HandleDefaultValue.php'
__DIR__ . '/Fixture/HandleDefaultValue.php',
], [[$argErrorMessage, 15]]];
}

Expand Down

0 comments on commit 06a5fb5

Please sign in to comment.