Skip to content

Commit

Permalink
[VendorLocker] Allow both in vendor for child and parent on ParentCla…
Browse files Browse the repository at this point in the history
…ssMethodTypeOverrideGuard (#1399)

* [VendorLocker] Allow both in vendor for child and parent on ParentClassMethodTypeOverrideGuard

* fix check

* add test from anonymous class to check that file name of current class method will always found
  • Loading branch information
samsonasik committed Dec 6, 2021
1 parent 29562a6 commit dda87fc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,32 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
return false;
}

/*
* Below verify that both current file name and parent file name is not in the /vendor/, if yes, then allowed.
* This can happen when rector run into /vendor/ directory while child and parent both are there.
*
* @see https://3v4l.org/Rc0RF#v8.0.13
*
* - both in /vendor/ -> allowed
* - one of them in /vendor/ -> not allowed
* - both not in /vendor/ -> allowed
*/
/** @var Scope $scope */
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
/** @var ClassReflection $currentClassReflection */
$currentClassReflection = $scope->getClassReflection();
/** @var string $currentFileName */
$currentFileName = $currentClassReflection->getFileName();

// child (current)
$normalizedCurrentFileName = $this->pathNormalizer->normalizePath($currentFileName);
$isCurrentInVendor = str_contains($normalizedCurrentFileName, '/vendor/');

// parent
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName);
return ! str_contains($normalizedFileName, '/vendor/');
$isParentInVendor = str_contains($normalizedFileName, '/vendor/');

return ($isCurrentInVendor && $isParentInVendor) || (! $isCurrentInVendor && ! $isParentInVendor);
}

public function hasParentClassMethod(ClassMethod $classMethod): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Exception;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source\MixedReturn;

new class implements MixedReturn {
public function run()
{
throw new Exception('test');
}
};

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Exception;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source\MixedReturn;

new class implements MixedReturn {
public function run(): never
{
throw new Exception('test');
}
};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source;

interface MixedReturn
{
public function run();
}

0 comments on commit dda87fc

Please sign in to comment.