Skip to content

ThrowsPhpDocRule: rule should not change AST (fixed #113) #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array
return [];
}

$classReflection = $scope->getClassReflection();
$classReflection = $scope->getTraitReflection();
if ($classReflection === null) {
$classReflection = $scope->getClassReflection();
}

if ($classReflection === null) {
try {
$methodReflection = $this->broker->getFunction(new Name($node->name->toString()), $scope);
Expand All @@ -489,7 +493,7 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array
if (!$node->hasAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END)) {
$node->setAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END, true);
if ($node->stmts === null) {
$node->stmts = [];
throw new ShouldNotHappenException();
}
$node->stmts[] = new FunctionEnd($node);
}
Expand Down
61 changes: 61 additions & 0 deletions tests/src/Rules/Bug113Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace Pepakriz\PHPStanExceptionRules\Rules;

use Pepakriz\PHPStanExceptionRules\CheckedExceptionService;
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeExtension;
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeService;
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
use Pepakriz\PHPStanExceptionRules\Rules\Data\CheckedException;
use Pepakriz\PHPStanExceptionRules\Rules\DynamicExtension\DynamicExtension;
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
use PHPStan\Rules\Rule;
use ReflectionException;
use RuntimeException;

/**
* @extends RuleTestCase<ThrowsPhpDocRule>
*/
class Bug113Test extends RuleTestCase
{

protected function getRule(): Rule
{
$defaultThrowTypeService = new DefaultThrowTypeService([], []);

$extensions = [
new DynamicExtension(),
new DefaultThrowTypeExtension($defaultThrowTypeService),
];

return new ThrowsPhpDocRule(
new CheckedExceptionService(
[
RuntimeException::class,
CheckedException::class,
ReflectionException::class,
]
),
new DynamicThrowTypeService(
$extensions,
$extensions,
$extensions,
$extensions
),
$defaultThrowTypeService,
$this->createThrowsAnnotationReader(),
$this->createBroker(),
false,
true,
false,
false,
[]
);
}

public function testBasicThrows(): void
{
$this->analyse(__DIR__ . '/data/bug113.php');
}

}
22 changes: 22 additions & 0 deletions tests/src/Rules/data/bug113.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace Pepakriz\PHPStanExceptionRules\Rules\Bug113;

trait FooTrait
{

abstract public function getString(): string;

}

class Foo
{

use FooTrait;

public function getString(): string
{
return 'Foo';
}

}