Skip to content
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

Enhancement: Add support for property hooks #915

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
72 changes: 72 additions & 0 deletions src/PropertyHooks/NoReturnByReferenceRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpstan-rules
*/

namespace Ergebnis\PHPStan\Rules\PropertyHooks;

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\Reflection;
use PHPStan\Rules;

/**
* @implements Rules\Rule<Node\PropertyHook>
*/
final class NoReturnByReferenceRule implements Rules\Rule
{
public function getNodeType(): string
{
return Node\PropertyHook::class;
}

public function processNode(
Node $node,
Analyser\Scope $scope
): array {
if (false === $node->byRef) {
return [];
}

$methodName = $node->name->toString();

/** @var Reflection\ClassReflection $classReflection */
$classReflection = $scope->getClassReflection();

if ($classReflection->isAnonymous()) {
$message = \sprintf(
'Property hook %s() in anonymous class returns by reference.',
$methodName,
);

return [
Rules\RuleErrorBuilder::message($message)
->identifier(ErrorIdentifier::noReturnByReference()->toString())
->build(),
];
}

$className = $classReflection->getName();

$message = \sprintf(
'Property hook %s::%s() returns by reference.',
$className,
$methodName,
);

return [
Rules\RuleErrorBuilder::message($message)
->identifier(ErrorIdentifier::noReturnByReference()->toString())
->build(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule;

final class PropertyHookInClass
{
private $foo {
get {
return $this->foo;
}
}
private $bar {
get {
return $this->bar;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule;

final class PropertyHookInClassReturningByReference
{
private $foo {
get {
return $this->foo;
}
}
private $bar {
&get {
return $this->bar;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule;

trait PropertyHookInTrait
{
private $foo {
get {
return $this->foo;
}
}
private $bar {
get {
return $this->bar;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule;

trait PropertyHookInTraitReturningByReference
{
private $foo {
get {
return $this->foo;
}
}
private $bar {
&get {
return $this->bar;
}
}
}
31 changes: 31 additions & 0 deletions test/Fixture/PropertyHooks/NoReturnByReferenceRule/script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule;

$foo = new class() {
private $foo {
get {
return $this->foo;
}
}
private $bar {
get {
return $this->bar;
}
}
};

$bar = new class() {
private $foo {
get {
return $this->foo;
}
}
private $bar {
&get {
return $this->bar;
}
}
};
56 changes: 56 additions & 0 deletions test/Integration/PropertyHooks/NoReturnByReferenceRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpstan-rules
*/

namespace Ergebnis\PHPStan\Rules\Test\Integration\PropertyHooks;

use Ergebnis\PHPStan\Rules\PropertyHooks;
use Ergebnis\PHPStan\Rules\Test;
use PHPStan\Rules;
use PHPStan\Testing;

/**
* @covers \Ergebnis\PHPStan\Rules\PropertyHooks\NoReturnByReferenceRule
*
* @uses \Ergebnis\PHPStan\Rules\ErrorIdentifier
*
* @extends Testing\RuleTestCase<PropertyHooks\NoReturnByReferenceRule>
*/
final class NoReturnByReferenceRuleTest extends Testing\RuleTestCase
{
use Test\Util\Helper;

public function testNoReturnByReferenceRule(): void
{
$this->analyse(
self::phpFilesIn(__DIR__ . '/../../Fixture/PropertyHooks/NoReturnByReferenceRule'),
[
[
\sprintf(
'Property hook %s::foo() returns by reference.',
Test\Fixture\PropertyHooks\NoReturnByReferenceRule\PropertyHookInClassReturningByReference::class,

Check failure on line 40 in test/Integration/PropertyHooks/NoReturnByReferenceRuleTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

Class Ergebnis\PHPStan\Rules\Test\Fixture\PropertyHooks\NoReturnByReferenceRule\PropertyHookInClassReturningByReference not found.
),
15,
],
[
'Property hook foo() in anonymous class returns by reference.',
27,
],
],
);
}

protected function getRule(): Rules\Rule
{
return new PropertyHooks\NoReturnByReferenceRule();
}
}
Loading