2020-05-02 18:52:16 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Naming\Naming;
|
2020-05-02 18:52:16 +00:00
|
|
|
|
2022-07-01 07:39:49 +00:00
|
|
|
use RectorPrefix202207\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\Generic\GenericObjectType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\StaticType;
|
|
|
|
use PHPStan\Type\ThisType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\TypeWithClassName;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\Util\StringUtils;
|
|
|
|
use Rector\Naming\RectorNamingInflector;
|
|
|
|
use Rector\Naming\ValueObject\ExpectedName;
|
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
|
2020-08-01 16:59:36 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @todo merge with very similar logic in
|
|
|
|
* @see VariableNaming
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Naming\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-07-01 21:41:49 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const INTERFACE = 'Interface';
|
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-07-19 18:19:25 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper
|
2020-07-19 18:19:25 +00:00
|
|
|
*/
|
|
|
|
private $typeUnwrapper;
|
2020-09-09 08:52:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\RectorNamingInflector
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2020-09-15 08:23:13 +00:00
|
|
|
private $rectorNamingInflector;
|
2021-01-15 20:27:36 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
2021-01-15 20:27:36 +00:00
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(TypeUnwrapper $typeUnwrapper, RectorNamingInflector $rectorNamingInflector, NodeTypeResolver $nodeTypeResolver)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-07-19 18:19:25 +00:00
|
|
|
$this->typeUnwrapper = $typeUnwrapper;
|
2020-09-15 08:23:13 +00:00
|
|
|
$this->rectorNamingInflector = $rectorNamingInflector;
|
2021-01-15 20:27:36 +00:00
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getExpectedNameFromMethodName(string $methodName) : ?ExpectedName
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$matches = Strings::match($methodName, self::GET_PREFIX_REGEX);
|
2020-09-09 08:52:43 +00:00
|
|
|
if ($matches === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-21 14:59:06 +00:00
|
|
|
$originalName = \lcfirst((string) $matches['root_name']);
|
2022-06-07 08:22:29 +00:00
|
|
|
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
|
2020-07-19 18:19:25 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +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);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof TypeWithClassName) {
|
2020-05-02 18:52:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof SelfObjectType) {
|
2020-05-02 18:52:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof StaticType) {
|
2020-05-02 18:52:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$className = $type instanceof AliasedObjectType ? $type->getClassName() : $this->nodeTypeResolver->getFullyQualifiedClassName($type);
|
2021-08-09 10:54:50 +00:00
|
|
|
// generic types are usually mix of parent type and specific type - various way to handle it
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof GenericObjectType) {
|
2021-08-09 10:54:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-05-02 18:52:16 +00:00
|
|
|
foreach (self::EXCLUDED_CLASSES as $excludedClass) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (StringUtils::isMatch($className, $excludedClass)) {
|
2020-05-02 18:52:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$shortClassName = $this->resolveShortClassName($className);
|
|
|
|
$shortClassName = $this->removePrefixesAndSuffixes($shortClassName);
|
|
|
|
// if all is upper-cased, it should be lower-cased
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($shortClassName === \strtoupper($shortClassName)) {
|
|
|
|
$shortClassName = \strtolower($shortClassName);
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
// remove "_"
|
2022-06-07 08:22:29 +00:00
|
|
|
$shortClassName = Strings::replace($shortClassName, '#_#', '');
|
2020-05-02 18:52:16 +00:00
|
|
|
$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);
|
2022-06-07 08:22:29 +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
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PHPStan\Type\ThisType|\PHPStan\Type\ObjectType|string $objectType
|
2020-07-01 21:41:49 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function fqnToVariableName($objectType) : string
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($objectType instanceof ThisType) {
|
2022-01-17 00:12:52 +00:00
|
|
|
$objectType = $objectType->getStaticObjectType();
|
|
|
|
}
|
2020-07-01 21:41:49 +00:00
|
|
|
$className = $this->resolveClassName($objectType);
|
2022-06-21 14:59:06 +00:00
|
|
|
$shortClassName = \strpos($className, '\\') !== \false ? (string) Strings::after($className, '\\', -1) : $className;
|
2022-01-17 00:12:52 +00:00
|
|
|
$variableName = $this->removeInterfaceSuffixPrefix($shortClassName, 'interface');
|
|
|
|
$variableName = $this->removeInterfaceSuffixPrefix($variableName, 'abstract');
|
|
|
|
$variableName = $this->fqnToShortName($variableName);
|
|
|
|
$variableName = \str_replace('_', '', $variableName);
|
2020-08-30 16:55:16 +00:00
|
|
|
// prolong too short generic names with one namespace up
|
2022-01-17 00:12:52 +00:00
|
|
|
return $this->prolongIfTooShort($variableName, $className);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-12 19:20:31 +00:00
|
|
|
* @see https://stackoverflow.com/a/2792045/1348344
|
2020-07-01 21:41:49 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function underscoreToName(string $underscoreName) : string
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$uppercaseWords = \ucwords($underscoreName, '_');
|
|
|
|
$pascalCaseName = \str_replace('_', '', $uppercaseWords);
|
|
|
|
return \lcfirst($pascalCaseName);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function resolveShortClassName(string $className) : string
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2021-05-29 22:10:59 +00:00
|
|
|
if (\strpos($className, '\\') !== \false) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return (string) Strings::after($className, '\\', -1);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
|
|
|
return $className;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function removePrefixesAndSuffixes(string $shortClassName) : string
|
2020-05-02 18:52:16 +00:00
|
|
|
{
|
|
|
|
// is SomeInterface
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\substr_compare($shortClassName, self::INTERFACE, -\strlen(self::INTERFACE)) === 0) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$shortClassName = Strings::substring($shortClassName, 0, -\strlen(self::INTERFACE));
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
// is ISomeClass
|
|
|
|
if ($this->isPrefixedInterface($shortClassName)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$shortClassName = Strings::substring($shortClassName, 1);
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
// is AbstractClass
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\strncmp($shortClassName, 'Abstract', \strlen('Abstract')) === 0) {
|
2022-07-02 22:36:41 +00:00
|
|
|
return Strings::substring($shortClassName, \strlen('Abstract'));
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|
|
|
|
return $shortClassName;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function normalizeUpperCase(string $shortClassName) : string
|
2020-05-02 18:52:16 +00:00
|
|
|
{
|
|
|
|
// turns $SOMEUppercase => $someUppercase
|
2021-05-09 20:15:43 +00:00
|
|
|
for ($i = 0; $i <= \strlen($shortClassName); ++$i) {
|
|
|
|
if (\ctype_upper($shortClassName[$i]) && $this->isNumberOrUpper($shortClassName[$i + 1])) {
|
|
|
|
$shortClassName[$i] = \strtolower($shortClassName[$i]);
|
2020-05-02 18:52:16 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $shortClassName;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function prolongIfTooShort(string $shortClassName, string $className) : string
|
2020-08-30 16:55:16 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\in_array($shortClassName, ['Factory', 'Repository'], \true)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$namespaceAbove = (string) Strings::after($className, '\\', -2);
|
|
|
|
$namespaceAbove = (string) Strings::before($namespaceAbove, '\\');
|
2021-05-09 20:15:43 +00:00
|
|
|
return \lcfirst($namespaceAbove) . $shortClassName;
|
2020-08-30 16:55:16 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \lcfirst($shortClassName);
|
2020-08-30 16:55:16 +00:00
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\Type\ObjectType|string $objectType
|
2020-08-05 20:45:36 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function resolveClassName($objectType) : string
|
2020-05-13 20:43:48 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($objectType instanceof ObjectType) {
|
2020-08-05 20:45:36 +00:00
|
|
|
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
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function fqnToShortName(string $fqn) : string
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2021-05-29 22:10:59 +00:00
|
|
|
if (\strpos($fqn, '\\') === \false) {
|
2020-07-01 21:41:49 +00:00
|
|
|
return $fqn;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$lastNamePart = Strings::after($fqn, '\\', -1);
|
2021-10-25 11:14:32 +00:00
|
|
|
if (!\is_string($lastNamePart)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new ShouldNotHappenException();
|
2021-10-25 11:14:32 +00:00
|
|
|
}
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\substr_compare($lastNamePart, self::INTERFACE, -\strlen(self::INTERFACE)) === 0) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return Strings::substring($lastNamePart, 0, -\strlen(self::INTERFACE));
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
|
|
|
return $lastNamePart;
|
|
|
|
}
|
2022-01-17 00:12:52 +00:00
|
|
|
private function removeInterfaceSuffixPrefix(string $className, string $category) : string
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2022-01-17 00:12:52 +00:00
|
|
|
// suffix
|
2022-06-07 08:22:29 +00:00
|
|
|
if (Strings::match($className, '#' . $category . '$#i')) {
|
|
|
|
return Strings::substring($className, 0, -\strlen($category));
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2022-01-17 00:12:52 +00:00
|
|
|
// prefix
|
2022-06-07 08:22:29 +00:00
|
|
|
if (Strings::match($className, '#^' . $category . '#i')) {
|
|
|
|
return Strings::substring($className, \strlen($category));
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2022-01-17 00:12:52 +00:00
|
|
|
// starts with "I\W+"?
|
2022-06-07 08:22:29 +00:00
|
|
|
if (StringUtils::isMatch($className, self::I_PREFIX_REGEX)) {
|
|
|
|
return Strings::substring($className, 1);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2022-01-17 00:12:52 +00:00
|
|
|
return $className;
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function isPrefixedInterface(string $shortClassName) : bool
|
2020-07-01 21:41:49 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\strlen($shortClassName) <= 3) {
|
|
|
|
return \false;
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\strncmp($shortClassName, 'I', \strlen('I')) !== 0) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\ctype_upper($shortClassName[1])) {
|
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \ctype_lower($shortClassName[2]);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function isNumberOrUpper(string $char) : bool
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\ctype_upper($char)) {
|
|
|
|
return \true;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \ctype_digit($char);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|