2020-05-02 18:52:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-02 21:25:24 +00:00
|
|
|
namespace Rector\Naming\Naming;
|
2020-05-02 18:52:16 +00:00
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
2021-03-01 01:28:35 +00:00
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2021-01-20 11:41:35 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2020-09-22 20:22:10 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2020-07-01 21:41:49 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
2020-05-02 18:52:16 +00:00
|
|
|
use PHPStan\Type\StaticType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\TypeWithClassName;
|
2020-09-22 20:22:10 +00:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-09-15 08:23:13 +00:00
|
|
|
use Rector\Naming\RectorNamingInflector;
|
2020-09-09 08:52:43 +00:00
|
|
|
use Rector\Naming\ValueObject\ExpectedName;
|
2020-08-01 16:59:36 +00:00
|
|
|
use Rector\NetteKdyby\Naming\VariableNaming;
|
2020-09-22 20:22:10 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-01-15 20:27:36 +00:00
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-07-19 18:19:25 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
|
2020-12-24 16:31:24 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
|
2020-05-02 18:52:16 +00:00
|
|
|
|
2020-08-01 16:59:36 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @todo merge with very similar logic in
|
|
|
|
* @see VariableNaming
|
2020-09-15 08:23:13 +00:00
|
|
|
* @see \Rector\Naming\Tests\Naming\PropertyNamingTest
|
2020-08-01 16:59:36 +00:00
|
|
|
*/
|
2020-05-02 18:52:16 +00:00
|
|
|
final class PropertyNaming
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-06-29 20:01:32 +00:00
|
|
|
private const EXCLUDED_CLASSES = ['#Closure#', '#^Spl#', '#FileInfo#', '#^std#', '#Iterator#', '#SimpleXML#'];
|
2020-05-02 18:52:16 +00:00
|
|
|
|
2020-07-01 21:41:49 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const INTERFACE = 'Interface';
|
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
/**
|
|
|
|
* @see https://regex101.com/r/RDhBNR/1
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const PREFIXED_CLASS_METHODS_REGEX = '#^(is|are|was|were|has|have|had|can)[A-Z].+#';
|
|
|
|
|
2020-09-23 09:16:40 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-29 18:04:33 +00:00
|
|
|
* @see https://regex101.com/r/U78rUF/1
|
2020-09-23 09:16:40 +00:00
|
|
|
*/
|
|
|
|
private const I_PREFIX_REGEX = '#^I[A-Z]#';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://regex101.com/r/hnU5pm/2/
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-12-24 22:16:27 +00:00
|
|
|
private const GET_PREFIX_REGEX = '#^get(?<root_name>[A-Z].+)#';
|
2020-09-23 09:16:40 +00:00
|
|
|
|
2020-07-19 18:19:25 +00:00
|
|
|
/**
|
|
|
|
* @var TypeUnwrapper
|
|
|
|
*/
|
|
|
|
private $typeUnwrapper;
|
|
|
|
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2020-09-15 08:23:13 +00:00
|
|
|
* @var RectorNamingInflector
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2020-09-15 08:23:13 +00:00
|
|
|
private $rectorNamingInflector;
|
2020-09-09 08:52:43 +00:00
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
/**
|
|
|
|
* @var BetterNodeFinder
|
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
|
2021-01-15 20:27:36 +00:00
|
|
|
/**
|
|
|
|
* @var NodeTypeResolver
|
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
|
|
|
* @var ReflectionProvider
|
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
public function __construct(
|
|
|
|
TypeUnwrapper $typeUnwrapper,
|
|
|
|
RectorNamingInflector $rectorNamingInflector,
|
|
|
|
BetterNodeFinder $betterNodeFinder,
|
2021-01-15 20:27:36 +00:00
|
|
|
NodeNameResolver $nodeNameResolver,
|
2021-02-28 07:47:48 +00:00
|
|
|
NodeTypeResolver $nodeTypeResolver,
|
|
|
|
ReflectionProvider $reflectionProvider
|
2020-09-22 20:22:10 +00:00
|
|
|
) {
|
2020-07-19 18:19:25 +00:00
|
|
|
$this->typeUnwrapper = $typeUnwrapper;
|
2020-09-15 08:23:13 +00:00
|
|
|
$this->rectorNamingInflector = $rectorNamingInflector;
|
2020-09-22 20:22:10 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2021-01-15 20:27:36 +00:00
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
2021-02-28 07:47:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName
|
|
|
|
{
|
2020-09-23 09:16:40 +00:00
|
|
|
$matches = Strings::match($methodName, self::GET_PREFIX_REGEX);
|
2020-09-09 08:52:43 +00:00
|
|
|
if ($matches === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-24 22:16:27 +00:00
|
|
|
$originalName = lcfirst($matches['root_name']);
|
2020-09-09 08:52:43 +00:00
|
|
|
|
2020-09-15 08:23:13 +00:00
|
|
|
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
|
2020-07-19 18:19:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 08:52:43 +00:00
|
|
|
public function getExpectedNameFromType(Type $type): ?ExpectedName
|
2020-05-02 18:52:16 +00:00
|
|
|
{
|
2020-12-06 00:27:00 +00:00
|
|
|
$type = $this->typeUnwrapper->unwrapNullableType($type);
|
2020-05-02 18:52:16 +00:00
|
|
|
if (! $type instanceof TypeWithClassName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof SelfObjectType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof StaticType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-15 20:27:36 +00:00
|
|
|
$className = $this->nodeTypeResolver->getFullyQualifiedClassName($type);
|
2020-05-02 18:52:16 +00:00
|
|
|
|
|
|
|
foreach (self::EXCLUDED_CLASSES as $excludedClass) {
|
|
|
|
if (Strings::match($className, $excludedClass)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$shortClassName = $this->resolveShortClassName($className);
|
|
|
|
$shortClassName = $this->removePrefixesAndSuffixes($shortClassName);
|
|
|
|
|
|
|
|
// if all is upper-cased, it should be lower-cased
|
|
|
|
if ($shortClassName === strtoupper($shortClassName)) {
|
|
|
|
$shortClassName = strtolower($shortClassName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove "_"
|
|
|
|
$shortClassName = Strings::replace($shortClassName, '#_#', '');
|
|
|
|
$shortClassName = $this->normalizeUpperCase($shortClassName);
|
|
|
|
|
2020-08-30 16:55:16 +00:00
|
|
|
// prolong too short generic names with one namespace up
|
2020-09-09 08:52:43 +00:00
|
|
|
$originalName = $this->prolongIfTooShort($shortClassName, $className);
|
2020-09-15 08:23:13 +00:00
|
|
|
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 21:41:49 +00:00
|
|
|
/**
|
|
|
|
* @param ObjectType|string $objectType
|
|
|
|
*/
|
|
|
|
public function fqnToVariableName($objectType): string
|
|
|
|
{
|
|
|
|
$className = $this->resolveClassName($objectType);
|
|
|
|
|
|
|
|
$shortName = $this->fqnToShortName($className);
|
|
|
|
$shortName = $this->removeInterfaceSuffixPrefix($className, $shortName);
|
|
|
|
|
2020-08-30 16:55:16 +00:00
|
|
|
// prolong too short generic names with one namespace up
|
|
|
|
return $this->prolongIfTooShort($shortName, $className);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @source https://stackoverflow.com/a/2792045/1348344
|
|
|
|
*/
|
|
|
|
public function underscoreToName(string $underscoreName): string
|
|
|
|
{
|
2021-01-24 20:12:02 +00:00
|
|
|
$uppercaseWords = ucwords($underscoreName, '_');
|
|
|
|
$pascalCaseName = str_replace('_', '', $uppercaseWords);
|
2020-07-01 21:41:49 +00:00
|
|
|
|
2020-08-28 16:07:15 +00:00
|
|
|
return lcfirst($pascalCaseName);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
public function getExpectedNameFromBooleanPropertyType(Property $property): ?string
|
|
|
|
{
|
|
|
|
$prefixedClassMethods = $this->getPrefixedClassMethods($property);
|
|
|
|
if ($prefixedClassMethods === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethods = $this->filterClassMethodsWithPropertyFetchReturnOnly($prefixedClassMethods, $property);
|
|
|
|
if (count($classMethods) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethod = reset($classMethods);
|
|
|
|
return $this->nodeNameResolver->getName($classMethod);
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
private function resolveShortClassName(string $className): string
|
|
|
|
{
|
|
|
|
if (Strings::contains($className, '\\')) {
|
|
|
|
return Strings::after($className, '\\', -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $className;
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:52:16 +00:00
|
|
|
private function removePrefixesAndSuffixes(string $shortClassName): string
|
|
|
|
{
|
|
|
|
// is SomeInterface
|
2020-09-23 09:16:40 +00:00
|
|
|
if (Strings::endsWith($shortClassName, self::INTERFACE)) {
|
|
|
|
$shortClassName = Strings::substring($shortClassName, 0, -strlen(self::INTERFACE));
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// is ISomeClass
|
|
|
|
if ($this->isPrefixedInterface($shortClassName)) {
|
|
|
|
$shortClassName = Strings::substring($shortClassName, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// is AbstractClass
|
|
|
|
if (Strings::startsWith($shortClassName, 'Abstract')) {
|
|
|
|
$shortClassName = Strings::substring($shortClassName, strlen('Abstract'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shortClassName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function normalizeUpperCase(string $shortClassName): string
|
|
|
|
{
|
|
|
|
// turns $SOMEUppercase => $someUppercase
|
|
|
|
for ($i = 0; $i <= strlen($shortClassName); ++$i) {
|
2020-05-13 20:43:48 +00:00
|
|
|
if (ctype_upper($shortClassName[$i]) && $this->isNumberOrUpper($shortClassName[$i + 1])) {
|
2020-05-02 18:52:16 +00:00
|
|
|
$shortClassName[$i] = strtolower($shortClassName[$i]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 20:43:48 +00:00
|
|
|
|
2020-05-02 18:52:16 +00:00
|
|
|
return $shortClassName;
|
|
|
|
}
|
2020-05-03 11:26:26 +00:00
|
|
|
|
2020-08-30 16:55:16 +00:00
|
|
|
private function prolongIfTooShort(string $shortClassName, string $className): string
|
|
|
|
{
|
|
|
|
if (in_array($shortClassName, ['Factory', 'Repository'], true)) {
|
2020-12-25 13:20:13 +00:00
|
|
|
$namespaceAbove = (string) Strings::after($className, '\\', -2);
|
|
|
|
$namespaceAbove = (string) Strings::before($namespaceAbove, '\\');
|
2020-08-30 16:55:16 +00:00
|
|
|
|
|
|
|
return lcfirst($namespaceAbove) . $shortClassName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lcfirst($shortClassName);
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
|
|
|
* @param ObjectType|string $objectType
|
|
|
|
*/
|
|
|
|
private function resolveClassName($objectType): string
|
2020-05-13 20:43:48 +00:00
|
|
|
{
|
2020-08-05 20:45:36 +00:00
|
|
|
if ($objectType instanceof ObjectType) {
|
|
|
|
return $objectType->getClassName();
|
2020-05-13 20:43:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
return $objectType;
|
2020-05-13 20:43:48 +00:00
|
|
|
}
|
2020-07-01 21:41:49 +00:00
|
|
|
|
|
|
|
private function fqnToShortName(string $fqn): string
|
|
|
|
{
|
|
|
|
if (! Strings::contains($fqn, '\\')) {
|
|
|
|
return $fqn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string $lastNamePart */
|
|
|
|
$lastNamePart = Strings::after($fqn, '\\', - 1);
|
|
|
|
if (Strings::endsWith($lastNamePart, self::INTERFACE)) {
|
|
|
|
return Strings::substring($lastNamePart, 0, - strlen(self::INTERFACE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $lastNamePart;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function removeInterfaceSuffixPrefix(string $className, string $shortName): string
|
|
|
|
{
|
|
|
|
// remove interface prefix/suffix
|
2021-02-28 07:47:48 +00:00
|
|
|
if (! $this->reflectionProvider->hasClass($className)) {
|
2020-07-01 21:41:49 +00:00
|
|
|
return $shortName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts with "I\W+"?
|
2020-09-23 09:16:40 +00:00
|
|
|
if (Strings::match($shortName, self::I_PREFIX_REGEX)) {
|
2020-07-01 21:41:49 +00:00
|
|
|
return Strings::substring($shortName, 1);
|
|
|
|
}
|
|
|
|
|
2020-09-23 09:16:40 +00:00
|
|
|
if (Strings::endsWith($shortName, self::INTERFACE)) {
|
2020-07-01 21:41:49 +00:00
|
|
|
return Strings::substring($shortName, -strlen(self::INTERFACE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shortName;
|
|
|
|
}
|
|
|
|
|
2020-09-22 20:22:10 +00:00
|
|
|
/**
|
|
|
|
* @return ClassMethod[]
|
|
|
|
*/
|
|
|
|
private function getPrefixedClassMethods(Property $property): array
|
|
|
|
{
|
|
|
|
$classLike = $property->getAttribute(AttributeKey::CLASS_NODE);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $classLike instanceof ClassLike) {
|
2020-09-22 20:22:10 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethods = $this->betterNodeFinder->findInstanceOf($classLike, ClassMethod::class);
|
|
|
|
return array_filter($classMethods, function (ClassMethod $classMethod): bool {
|
2021-03-01 01:28:35 +00:00
|
|
|
return $this->isBoolishMethodName($classMethod);
|
2020-09-22 20:22:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassMethod[] $prefixedClassMethods
|
|
|
|
* @return ClassMethod[]
|
|
|
|
*/
|
|
|
|
private function filterClassMethodsWithPropertyFetchReturnOnly(
|
|
|
|
array $prefixedClassMethods,
|
|
|
|
Property $property
|
|
|
|
): array {
|
2021-03-01 01:28:35 +00:00
|
|
|
$classMethodName = $this->nodeNameResolver->getName($property);
|
2020-09-22 20:22:10 +00:00
|
|
|
|
2021-03-01 01:28:35 +00:00
|
|
|
return array_filter($prefixedClassMethods, function (ClassMethod $classMethod) use ($classMethodName): bool {
|
|
|
|
return $this->doesClassMethodMatchReturnPropertyFetch($classMethod, $classMethodName);
|
2020-09-22 20:22:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
private function isPrefixedInterface(string $shortClassName): bool
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2020-08-05 20:45:36 +00:00
|
|
|
if (strlen($shortClassName) <= 3) {
|
|
|
|
return false;
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
if (! Strings::startsWith($shortClassName, 'I')) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-19 15:24:53 +00:00
|
|
|
if (! ctype_upper($shortClassName[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ctype_lower($shortClassName[2]);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function isNumberOrUpper(string $char): bool
|
|
|
|
{
|
|
|
|
if (ctype_upper($char)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctype_digit($char);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2021-03-01 01:28:35 +00:00
|
|
|
|
|
|
|
private function isBoolishMethodName(ClassMethod $classMethod): bool
|
|
|
|
{
|
|
|
|
$classMethodName = $this->nodeNameResolver->getName($classMethod);
|
|
|
|
return (bool) Strings::match($classMethodName, self::PREFIXED_CLASS_METHODS_REGEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doesClassMethodMatchReturnPropertyFetch(
|
|
|
|
ClassMethod $classMethod,
|
|
|
|
string $currentClassMethodName
|
|
|
|
): bool {
|
|
|
|
$possibleReturn = $classMethod->stmts[0] ?? null;
|
|
|
|
if (! $possibleReturn instanceof Return_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = $possibleReturn->expr;
|
|
|
|
if (! $node instanceof PropertyFetch) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nodeNameResolver->isName($node->name, $currentClassMethodName);
|
|
|
|
}
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|