-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add RectorAssert to warn about invalid class names (#1339)
- Loading branch information
1 parent
50bbdd1
commit 23679ac
Showing
11 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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\Core\Validation; | ||
|
||
use Rector\Core\Util\StringUtils; | ||
use Webmozart\Assert\InvalidArgumentException; | ||
|
||
/** | ||
* @see \Rector\Core\Tests\Validation\RectorAssertTest | ||
*/ | ||
final class RectorAssert | ||
{ | ||
/** | ||
* @see https://stackoverflow.com/a/12011255/1348344 | ||
* @see https://regex101.com/r/PYQaPF/1 | ||
* @var string | ||
*/ | ||
public const CLASS_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$#'; | ||
/** | ||
* Assert value is valid class name | ||
*/ | ||
public static function className(string $className): void | ||
{ | ||
if (StringUtils::isMatch($className, self::CLASS_NAME_REGEX)) { | ||
return; | ||
} | ||
$errorMessage = $className . ' is not a valid class name'; | ||
throw new InvalidArgumentException($errorMessage); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Validation; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\Core\Validation\RectorAssert; | ||
use Webmozart\Assert\InvalidArgumentException; | ||
|
||
final class RectorAssertTest extends TestCase | ||
{ | ||
/** | ||
* @doesNotPerformAssertions | ||
* @dataProvider provideDataValidClassNames() | ||
*/ | ||
public function testValidClasNames(string $className): void | ||
{ | ||
RectorAssert::className($className); | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataInvalidClassNames() | ||
*/ | ||
public function testInvalidClasNames(string $className): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
RectorAssert::className($className); | ||
} | ||
|
||
/** | ||
* @return Iterator<string[]> | ||
*/ | ||
public function provideDataValidClassNames(): Iterator | ||
{ | ||
yield ['App']; | ||
yield ['App\\SomeClass']; | ||
} | ||
|
||
/** | ||
* @return Iterator<string[]> | ||
*/ | ||
public function provideDataInvalidClassNames(): Iterator | ||
{ | ||
yield ['App Some']; | ||
yield ['App$SomeClass']; | ||
yield ['$SomeClass']; | ||
yield ['App\\\\Some']; | ||
yield ['3AppSome']; | ||
} | ||
} |