Updated Rector to commit 6577da612c2824c2add06721b903de4cd438631e

6577da612c [DX] Add input validation for method, property and function name to avoid invalid output ast (#2668)
This commit is contained in:
Tomas Votruba 2022-07-16 09:41:53 +00:00
parent 587cd1ea1b
commit 4b2bd56de7
31 changed files with 128 additions and 17 deletions

View File

@ -6,6 +6,7 @@ namespace Rector\Naming\ValueObject;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Validation\RectorAssert;
use Rector\Naming\Contract\RenamePropertyValueObjectInterface;
final class PropertyRename implements RenamePropertyValueObjectInterface
{
@ -47,6 +48,9 @@ final class PropertyRename implements RenamePropertyValueObjectInterface
$this->classLike = $classLike;
$this->classLikeName = $classLikeName;
$this->propertyProperty = $propertyProperty;
// name must be valid
RectorAssert::propertyName($currentName);
RectorAssert::propertyName($expectedName);
}
public function getProperty() : Property
{

View File

@ -28,6 +28,8 @@ final class RenameProperty
$this->oldProperty = $oldProperty;
$this->newProperty = $newProperty;
RectorAssert::className($type);
RectorAssert::propertyName($oldProperty);
RectorAssert::propertyName($newProperty);
}
public function getObjectType() : ObjectType
{

View File

@ -34,6 +34,7 @@ final class ArgumentFuncCallToMethodCall implements ArgumentFuncCallToMethodCall
$this->methodIfArgs = $methodIfArgs;
$this->methodIfNoArgs = $methodIfNoArgs;
RectorAssert::className($class);
RectorAssert::functionName($function);
}
public function getFunction() : string
{

View File

@ -40,6 +40,7 @@ final class ArrayFuncCallToMethodCall implements ArgumentFuncCallToMethodCallInt
$this->arrayMethod = $arrayMethod;
$this->nonArrayMethod = $nonArrayMethod;
RectorAssert::className($class);
RectorAssert::functionName($function);
}
public function getFunction() : string
{

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class DimFetchAssignToMethodCall
{
/**
@ -26,6 +27,7 @@ final class DimFetchAssignToMethodCall
$this->listClass = $listClass;
$this->itemClass = $itemClass;
$this->addMethod = $addMethod;
RectorAssert::methodName($addMethod);
}
public function getListObjectType() : ObjectType
{

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class FuncCallToMethodCall
{
/**
@ -26,6 +27,9 @@ final class FuncCallToMethodCall
$this->oldFuncName = $oldFuncName;
$this->newClassName = $newClassName;
$this->newMethodName = $newMethodName;
RectorAssert::functionName($oldFuncName);
RectorAssert::className($newClassName);
RectorAssert::methodName($newMethodName);
}
public function getOldFuncName() : string
{

View File

@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use Rector\Core\Validation\RectorAssert;
final class FuncCallToStaticCall
{
/**
@ -25,6 +26,9 @@ final class FuncCallToStaticCall
$this->oldFuncName = $oldFuncName;
$this->newClassName = $newClassName;
$this->newMethodName = $newMethodName;
RectorAssert::functionName($oldFuncName);
RectorAssert::className($newClassName);
RectorAssert::methodName($newMethodName);
}
public function getOldFuncName() : string
{

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class GetAndSetToMethodCall
{
/**
@ -29,6 +30,8 @@ final class GetAndSetToMethodCall
$this->classType = $classType;
$this->getMethod = $getMethod;
$this->setMethod = $setMethod;
RectorAssert::methodName($getMethod);
RectorAssert::methodName($setMethod);
}
public function getGetMethod() : string
{

View File

@ -37,6 +37,8 @@ final class MethodCallToAnotherMethodCallWithArguments
$this->newMethod = $newMethod;
$this->newArguments = $newArguments;
RectorAssert::className($type);
RectorAssert::methodName($oldMethod);
RectorAssert::methodName($newMethod);
}
public function getObjectType() : ObjectType
{

View File

@ -37,7 +37,9 @@ final class MethodCallToMethodCall
$this->newType = $newType;
$this->newMethod = $newMethod;
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);
RectorAssert::className($newType);
RectorAssert::methodName($newMethod);
}
public function getOldType() : string
{

View File

@ -28,6 +28,8 @@ final class MethodCallToPropertyFetch
$this->oldMethod = $oldMethod;
$this->newProperty = $newProperty;
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);
RectorAssert::propertyName($newProperty);
}
public function getOldObjectType() : ObjectType
{

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class MethodCallToStaticCall
{
/**
@ -32,6 +33,10 @@ final class MethodCallToStaticCall
$this->oldMethod = $oldMethod;
$this->newClass = $newClass;
$this->newMethod = $newMethod;
RectorAssert::className($oldClass);
RectorAssert::className($oldMethod);
RectorAssert::className($newClass);
RectorAssert::className($newMethod);
}
public function getOldObjectType() : ObjectType
{

View File

@ -13,6 +13,7 @@ final class NewArgToMethodCall
*/
private $type;
/**
* @readonly
* @var mixed
*/
private $value;
@ -30,6 +31,7 @@ final class NewArgToMethodCall
$this->value = $value;
$this->methodCall = $methodCall;
RectorAssert::className($type);
RectorAssert::className($methodCall);
}
public function getObjectType() : ObjectType
{

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class NewToMethodCall
{
/**
@ -26,6 +27,9 @@ final class NewToMethodCall
$this->newType = $newType;
$this->serviceType = $serviceType;
$this->serviceMethod = $serviceMethod;
RectorAssert::className($newType);
RectorAssert::className($serviceType);
RectorAssert::methodName($serviceMethod);
}
public function getNewObjectType() : ObjectType
{

View File

@ -29,6 +29,7 @@ final class NewToStaticCall
$this->staticCallMethod = $staticCallMethod;
RectorAssert::className($type);
RectorAssert::className($staticCallClass);
RectorAssert::methodName($staticCallMethod);
}
public function getObjectType() : ObjectType
{

View File

@ -3,6 +3,8 @@
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use Rector\Core\Validation\RectorAssert;
use RectorPrefix202207\Webmozart\Assert\Assert;
final class ParentClassToTraits
{
/**
@ -22,6 +24,8 @@ final class ParentClassToTraits
{
$this->parentType = $parentType;
$this->traitNames = $traitNames;
RectorAssert::className($parentType);
Assert::allString($traitNames);
}
public function getParentType() : string
{

View File

@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use Rector\Core\Validation\RectorAssert;
final class PropertyAndClassMethodName
{
/**
@ -19,6 +20,8 @@ final class PropertyAndClassMethodName
{
$this->propertyName = $propertyName;
$this->classMethodName = $classMethodName;
RectorAssert::propertyName($propertyName);
RectorAssert::methodName($classMethodName);
}
public function getPropertyName() : string
{

View File

@ -28,6 +28,8 @@ final class PropertyAssignToMethodCall
$this->oldPropertyName = $oldPropertyName;
$this->newMethodName = $newMethodName;
RectorAssert::className($class);
RectorAssert::propertyName($oldPropertyName);
RectorAssert::methodName($newMethodName);
}
public function getObjectType() : ObjectType
{

View File

@ -43,6 +43,11 @@ final class PropertyFetchToMethodCall
$this->newSetMethod = $newSetMethod;
$this->newGetArguments = $newGetArguments;
RectorAssert::className($oldType);
RectorAssert::propertyName($oldProperty);
RectorAssert::methodName($newGetMethod);
if (\is_string($newSetMethod)) {
RectorAssert::methodName($newSetMethod);
}
}
public function getOldObjectType() : ObjectType
{

View File

@ -28,6 +28,8 @@ final class ReplaceParentCallByPropertyCall
$this->method = $method;
$this->property = $property;
RectorAssert::className($class);
RectorAssert::methodName($method);
RectorAssert::propertyName($property);
}
public function getObjectType() : ObjectType
{

View File

@ -28,6 +28,7 @@ final class ServiceGetterToConstructorInjection
$this->oldMethod = $oldMethod;
$this->serviceType = $serviceType;
RectorAssert::className($oldType);
RectorAssert::methodName($oldMethod);
RectorAssert::className($serviceType);
}
public function getOldObjectType() : ObjectType

View File

@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use Rector\Core\Validation\RectorAssert;
final class StaticCallRecipe
{
/**
@ -19,6 +20,8 @@ final class StaticCallRecipe
{
$this->className = $className;
$this->methodName = $methodName;
RectorAssert::className($className);
RectorAssert::methodName($methodName);
}
public function getClassName() : string
{

View File

@ -28,6 +28,8 @@ final class StaticCallToFuncCall
$this->method = $method;
$this->function = $function;
RectorAssert::className($class);
RectorAssert::methodName($method);
RectorAssert::functionName($function);
}
public function getObjectType() : ObjectType
{

View File

@ -7,6 +7,7 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class StaticCallToMethodCall
{
/**
@ -35,6 +36,15 @@ final class StaticCallToMethodCall
$this->staticMethod = $staticMethod;
$this->classType = $classType;
$this->methodName = $methodName;
RectorAssert::className($staticClass);
// special char to match all method names
if ($staticMethod !== '*') {
RectorAssert::methodName($staticMethod);
}
RectorAssert::className($classType);
if ($methodName !== '*') {
RectorAssert::methodName($methodName);
}
}
public function getClassObjectType() : ObjectType
{

View File

@ -21,6 +21,7 @@ final class StaticCallToNew
$this->class = $class;
$this->method = $method;
RectorAssert::className($class);
RectorAssert::methodName($method);
}
public function getClass() : string
{

View File

@ -28,6 +28,8 @@ final class UnsetAndIssetToMethodCall
$this->issetMethodCall = $issetMethodCall;
$this->unsedMethodCall = $unsedMethodCall;
RectorAssert::className($type);
RectorAssert::methodName($issetMethodCall);
RectorAssert::methodName($unsedMethodCall);
}
public function getObjectType() : ObjectType
{

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e8d0d572934d5faec64d480d9cd985a3823c9c0a';
public const PACKAGE_VERSION = '6577da612c2824c2add06721b903de4cd438631e';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-07-16 01:10:56';
public const RELEASE_DATE = '2022-07-16 11:36:43';
/**
* @var int
*/

View File

@ -16,15 +16,52 @@ final class RectorAssert
* @var string
*/
private 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]*)*$#';
/**
* @see https://www.php.net/manual/en/language.variables.basics.php
* @see https://regex101.com/r/hFw17T/1
*
* @var string
*/
private const PROPERTY_NAME_REGEX = '#^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$#';
/**
* @see https://regex101.com/r/uh5B0S/1
* @see https://www.php.net/manual/en/functions.user-defined.php
*
* @var string
*/
private const METHOD_NAME_REGEX = '#^[a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*$#';
/**
* @see https://regex101.com/r/uh5B0S/1
* @see https://www.php.net/manual/en/functions.user-defined.php
*
* @var string
*/
private const FUNCTION_NAME_REGEX = '#([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(\\\\[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]))?([a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*)$#';
/**
* Assert value is valid class name
*/
public static function className(string $className) : void
public static function className(string $name) : void
{
if (StringUtils::isMatch($className, self::CLASS_NAME_REGEX)) {
self::elementName($name, self::CLASS_NAME_REGEX, 'class');
}
public static function propertyName(string $name) : void
{
self::elementName($name, self::PROPERTY_NAME_REGEX, 'property');
}
public static function methodName(string $name) : void
{
self::elementName($name, self::METHOD_NAME_REGEX, 'method');
}
public static function functionName(string $name) : void
{
self::elementName($name, self::FUNCTION_NAME_REGEX, 'function');
}
public static function elementName(string $name, string $regex, string $elementType) : void
{
if (StringUtils::isMatch($name, $regex)) {
return;
}
$errorMessage = $className . ' is not a valid class name';
$errorMessage = \sprintf('"%s" is not a valid %s name', $name, $elementType);
throw new InvalidArgumentException($errorMessage);
}
}

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2::getLoader();
return ComposerAutoloaderInite34fa8e76f36c03101d38fd93f034d0c::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2
class ComposerAutoloaderInite34fa8e76f36c03101d38fd93f034d0c
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInite34fa8e76f36c03101d38fd93f034d0c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInite34fa8e76f36c03101d38fd93f034d0c', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirec7a83898e2099d2f9e00f124fc0c36b2($fileIdentifier, $file);
composerRequiree34fa8e76f36c03101d38fd93f034d0c($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInitc7a83898e2099d2f9e00f124fc0c36b2
* @param string $file
* @return void
*/
function composerRequirec7a83898e2099d2f9e00f124fc0c36b2($fileIdentifier, $file)
function composerRequiree34fa8e76f36c03101d38fd93f034d0c($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2
class ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3416,9 +3416,9 @@ class ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc7a83898e2099d2f9e00f124fc0c36b2::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite34fa8e76f36c03101d38fd93f034d0c::$classMap;
}, null, ClassLoader::class);
}