-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new rule to add explicit public scope to unscoped class methods (#…
…2022) Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
d179903
commit 6ee3f4e
Showing
4 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...ector/ClassMethod/ExplicitPublicClassMethodRector/ExplicitPublicClassMethodRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ExplicitPublicClassMethodRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} | ||
|
63 changes: 63 additions & 0 deletions
63
...sts/Visibility/Rector/ClassMethod/ExplicitPublicClassMethodRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
class SomeClass | ||
{ | ||
function noExplicitVis() | ||
{ | ||
$a = 3; | ||
$closure = function ($b) use ($a) { | ||
return $a + $b; | ||
}; | ||
return $closure; | ||
} | ||
|
||
public function publicVis() | ||
{ | ||
} | ||
|
||
protected function protectedVis() | ||
{ | ||
} | ||
|
||
private function privateVis() | ||
{ | ||
} | ||
} | ||
|
||
function notInScope() | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
class SomeClass | ||
{ | ||
public function noExplicitVis() | ||
{ | ||
$a = 3; | ||
$closure = function ($b) use ($a) { | ||
return $a + $b; | ||
}; | ||
return $closure; | ||
} | ||
|
||
public function publicVis() | ||
{ | ||
} | ||
|
||
protected function protectedVis() | ||
{ | ||
} | ||
|
||
private function privateVis() | ||
{ | ||
} | ||
} | ||
|
||
function notInScope() | ||
{ | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
.../Visibility/Rector/ClassMethod/ExplicitPublicClassMethodRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Core\ValueObject\Visibility; | ||
use Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(ExplicitPublicClassMethodRector::class); | ||
}; | ||
|
80 changes: 80 additions & 0 deletions
80
rules/Visibility/Rector/ClassMethod/ExplicitPublicClassMethodRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Visibility\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\Privatization\NodeManipulator\VisibilityManipulator; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector\ExplicitPublicClassMethodRectorTest | ||
*/ | ||
final class ExplicitPublicClassMethodRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly VisibilityManipulator $visibilityManipulator, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Add explicit public method visibility.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
function foo() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function foo() | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// already non-public | ||
if (! $node->isPublic()) { | ||
return null; | ||
} | ||
|
||
// explicitly public | ||
if ($this->visibilityManipulator->hasVisibility($node, \Rector\Core\ValueObject\Visibility::PUBLIC)) { | ||
return null; | ||
} | ||
|
||
$this->visibilityManipulator->makePublic($node); | ||
|
||
return $node; | ||
} | ||
} |