Skip to content

Commit

Permalink
Implement a logic handling Property Hooks on PHP 8.4 or above in cl…
Browse files Browse the repository at this point in the history
…asses
  • Loading branch information
jakubtobiasz committed Nov 27, 2024
1 parent 86ff593 commit 469294a
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/property-hooks-bodies-in-interface.php \
--exclude tests/PHPStan/Rules/Properties/data/property-hooks-in-interface.php \
--exclude tests/PHPStan/Rules/Properties/data/property-hooks-visibility-in-interface.php \
--exclude tests/PHPStan/Rules/Properties/data/abstract-hooked-properties-in-class.php \
--exclude tests/PHPStan/Rules/Properties/data/abstract-hooked-properties-with-bodies \
--exclude tests/PHPStan/Rules/Properties/data/abstract-non-hooked-properties-in-abstract-class.php \
--exclude tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-abstract-class.php \
--exclude tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-class.php \
src tests

cs:
Expand Down
9 changes: 7 additions & 2 deletions build/collision-detector.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"../tests/PHPStan/Levels/data/stubs/function.php",
"../tests/PHPStan/Rules/Properties/data/property-hooks-bodies-in-interface.php",
"../tests/PHPStan/Rules/Properties/data/property-hooks-in-interface.php",
"../tests/PHPStan/Rules/Properties/data/property-hooks-visibility-in-interface.php"
]
"../tests/PHPStan/Rules/Properties/data/property-hooks-visibility-in-interface.php",
"../tests/PHPStan/Rules/Properties/data/abstract-hooked-properties-in-class.php",
"../tests/PHPStan/Rules/Properties/data/abstract-hooked-properties-with-bodies",
"../tests/PHPStan/Rules/Properties/data/abstract-non-hooked-properties-in-abstract-class.php",
"../tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-abstract-class.php",
"../tests/PHPStan/Rules/Properties/data/non-abstract-hooked-properties-in-class.php"
]
}
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ rules:
- PHPStan\Rules\Properties\MissingReadOnlyByPhpDocPropertyAssignRule
- PHPStan\Rules\Properties\PropertiesInInterfaceRule
- PHPStan\Rules\Properties\PropertyAttributesRule
- PHPStan\Rules\Properties\PropertyInClassRule
- PHPStan\Rules\Properties\ReadOnlyPropertyRule
- PHPStan\Rules\Properties\ReadOnlyByPhpDocPropertyRule
- PHPStan\Rules\Regexp\RegularExpressionPatternRule
Expand Down
5 changes: 5 additions & 0 deletions src/Node/ClassPropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public function isAllowedPrivateMutation(): bool
return $this->isAllowedPrivateMutation;
}

public function isAbstract(): bool
{
return (bool) ($this->flags & Modifiers::ABSTRACT);
}

public function getNativeType(): ?Type
{
return $this->type;
Expand Down
109 changes: 109 additions & 0 deletions src/Rules/Properties/PropertyInClassRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<ClassPropertyNode>
*/
final class PropertyInClassRule implements Rule
{

public function __construct(private PhpVersion $phpVersion)

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.

Check failure on line 18 in src/Rules/Properties/PropertyInClassRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Property PHPStan\Rules\Properties\PropertyInClassRule::$phpVersion is never read, only written.
{
}

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

public function processNode(Node $node, Scope $scope): array
{
$classReflection = $node->getClassReflection();

if (!$classReflection->isClass()) {
return [];
}

if (!$classReflection->isAbstract() && $node->hasHooks() && $node->isAbstract()) {
return [
RuleErrorBuilder::message('Classes may not include abstract hooked properties.')
->nonIgnorable()
->identifier('property.abstractHookedInClass')
->build(),
];
}

if (!$classReflection->isAbstract() && $node->hasHooks() && !$this->doAllHooksHaveBody($node)) {
return [
RuleErrorBuilder::message('Classes may not include hooked properties without bodies.')
->nonIgnorable()
->identifier('property.hookedWithoutBodyInClass')
->build(),
];
}

if (!$classReflection->isAbstract()) {
return [];
}

if ($node->hasHooks() && !$node->isAbstract()) {
return [
RuleErrorBuilder::message('Abstract classes may not include non-abstract hooked properties without bodies.')
->nonIgnorable()
->identifier('property.nonAbstractHookedWithoutBodyInAbstractClass')
->build(),
];
}

if ($node->isAbstract() && !$node->hasHooks()) {
return [
RuleErrorBuilder::message('Only hooked properties may be declared abstract.')
->nonIgnorable()
->identifier('property.nonHookedAbstractInClass')
->build(),
];
}

if ($node->isAbstract() && !$this->isAtLeastOneHookBodyEmpty($node)) {
return [
RuleErrorBuilder::message('Abstract properties must specify at least one abstract hook.')
->nonIgnorable()
->identifier('property.hookedAbstractWithBodies')
->build(),
];
}

return [];
}

private function doAllHooksHaveBody(ClassPropertyNode $node): bool
{
foreach ($node->getHooks() as $hook) {
if ($hook->body === null) {
return false;
}
}

return true;
}

private function isAtLeastOneHookBodyEmpty(ClassPropertyNode $node): bool
{
foreach ($node->getHooks() as $hook) {
if ($hook->body === null) {
return true;
}
}

return false;
}

}
93 changes: 93 additions & 0 deletions tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<PropertyInClassRule>
*/
class PropertyInClassRuleTest extends RuleTestCase
{

private int $phpVersion = PHP_VERSION_ID;

protected function getRule(): Rule
{
return new PropertyInClassRule(new PhpVersion($this->phpVersion));
}

public function testPhp84AndNonAbstractHookedPropertiesInClass(): void
{
$this->analyse([__DIR__ . '/data/non-abstract-hooked-properties-in-class.php'], [
[
'Classes may not include hooked properties without bodies.',
7,
],
[
'Classes may not include hooked properties without bodies.',
9,
],
]);
}

public function testPhp84AndAbstractHookedPropertiesInClass(): void
{
$this->analyse([__DIR__ . '/data/abstract-hooked-properties-in-class.php'], [
[
'Classes may not include abstract hooked properties.',
7,
],
[
'Classes may not include abstract hooked properties.',
9,
],
]);
}

public function testPhp84AndNonAbstractHookedPropertiesInAbstractClass(): void
{
$this->analyse([__DIR__ . '/data/non-abstract-hooked-properties-in-abstract-class.php'], [
[
'Abstract classes may not include non-abstract hooked properties without bodies.',
7,
],
[
'Abstract classes may not include non-abstract hooked properties without bodies.',
9,
],
]);
}

public function testPhp84AndAbstractNonHookedPropertiesInAbstractClass(): void
{
$this->analyse([__DIR__ . '/data/abstract-non-hooked-properties-in-abstract-class.php'], [
[
'Only hooked properties may be declared abstract.',
7,
],
[
'Only hooked properties may be declared abstract.',
9,
],
]);
}

public function testPhp84AndAbstractHookedPropertiesWithBodies(): void
{
$this->analyse([__DIR__ . '/data/abstract-hooked-properties-with-bodies.php'], [
[
'Abstract properties must specify at least one abstract hook.',
7,
],
[
'Abstract properties must specify at least one abstract hook.',
12,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace NonAbstractHookedPropertiesInAbstractClass;

class AbstractPerson
{
public abstract string $name { get; set; }

public abstract string $lastName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace AbstractHookedPropertiesWithBodies;

abstract class AbstractPerson
{
public abstract string $name {
get => $this->name;
set => $this->name = $value;
}

public abstract string $lastName {
get => $this->lastName;
set => $this->lastName = $value;
}

public abstract string $middleName {
get => $this->name;
set;
}

public abstract string $familyName {
get;
set;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace NonAbstractHookedPropertiesInAbstractClass;

abstract class AbstractPerson
{
public abstract string $name;

public abstract string $lastName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace NonAbstractHookedPropertiesInAbstractClass;

abstract class AbstractPerson
{
public string $name { get; set; }

public string $lastName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace NonAbstractHookedPropertiesInClass;

class Person
{
public string $name { get; set; }

public string $lastName { get; set; }
}

0 comments on commit 469294a

Please sign in to comment.