Skip to content

Commit

Permalink
fix reporting on renamed proeprty match type (#3098)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 26, 2022
1 parent 07b529a commit 1f8c88a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,6 @@ parameters:
-
path: packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php
message: '#Offset (.*?)includes(.*?) always exists and is not nullable#'

# returns bool for notifications
- '#Method "renamePropertyPromotion\(\)" returns bool type, so the name should start with is/has/was#'
11 changes: 8 additions & 3 deletions rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public function __construct(
) {
}

public function renamePropertyPromotion(Class_|Interface_ $classLike): void
public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
{
$hasChanged = false;

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::PROPERTY_PROMOTION)) {
return;
return false;
}

$constructClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return;
return false;
}

// resolve possible and existing param names
Expand All @@ -71,7 +73,10 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): void
}

$this->renameParamVarNameAndVariableUsage($classLike, $constructClassMethod, $desiredPropertyName, $param);
$hasChanged = true;
}

return $hasChanged;
}

private function renameParamVarNameAndVariableUsage(
Expand Down
12 changes: 8 additions & 4 deletions rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$this->refactorClassProperties($node);
$this->propertyPromotionRenamer->renamePropertyPromotion($node);

if (! $this->hasChanged) {
return null;
$hasPromotedPropertyChanged = $this->propertyPromotionRenamer->renamePropertyPromotion($node);
if ($this->hasChanged) {
return $node;
}

return $node;
if ($hasPromotedPropertyChanged) {
return $node;
}

return null;
}

private function refactorClassProperties(ClassLike $classLike): void
Expand Down

0 comments on commit 1f8c88a

Please sign in to comment.