Skip to content
Closed
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
37 changes: 37 additions & 0 deletions src/Usages/Reproducer2Usages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Usages;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node>
*/
class Reproducer2Usages implements Rule
{

public function getNodeType(): string
{
return FullyQualified::class;
}


/**
* @param FullyQualified $node
* @param Scope $scope
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
return [
RuleErrorBuilder::message(sprintf('class %s found in method %s', $node->toString(), $scope->getFunction()?->getName() ?? 'null'))->build(),

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan, --prefer-lowest)

Syntax error, unexpected T_OBJECT_OPERATOR on line 33

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan, --prefer-lowest)

Syntax error, unexpected ',' on line 33

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan, --prefer-lowest)

Syntax error, unexpected ')', expecting ']' on line 33

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan)

Syntax error, unexpected T_OBJECT_OPERATOR on line 33

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan)

Syntax error, unexpected ',' on line 33

Check failure on line 33 in src/Usages/Reproducer2Usages.php

View workflow job for this annotation

GitHub Actions / lint-phpcs-phpstan (7.4, composer phpstan)

Syntax error, unexpected ')', expecting ']' on line 33
];
}

}
46 changes: 46 additions & 0 deletions tests/Usages/Reproducer2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed\Usages;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

class Reproducer2Test extends RuleTestCase
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read this test correctly it is now independent from the disallowed-calls extension package, right?

if so, I think we should open a phpstan-src issue and add the reproducer there

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will port it over into the fixing PR at phpstan/phpstan-src#3944

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, and yes, it is independent now.

{

protected function shouldNarrowMethodScopeFromConstructor(): bool
{
return true;
}


protected function getRule(): Rule
{
return new Reproducer2Usages();
}


public function testRule(): void
{
$this->analyse([__DIR__ . '/../src/Reproducer2Class.php'], [
[
'class DateTime found in method null',
12,
],
[
'class Baz\Waldo found in method null',
16,
],
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
];
}

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

namespace Reproducer2;

class Reproducer2Class
{
public function __construct()
{
}

public function foo(\DateTime $d): void
{
}

public function bar(\Baz\Waldo $d): void
{
}
}
Loading