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;
|
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-05-03 11:26:26 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
2020-08-01 16:59:36 +00:00
|
|
|
use Rector\NetteKdyby\Naming\VariableNaming;
|
2020-05-02 18:52:16 +00:00
|
|
|
use Rector\PHPStan\Type\SelfObjectType;
|
|
|
|
use Rector\PHPStan\Type\ShortenedObjectType;
|
2020-07-19 18:19:25 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
|
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-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-07-19 18:19:25 +00:00
|
|
|
/**
|
|
|
|
* @var TypeUnwrapper
|
|
|
|
*/
|
|
|
|
private $typeUnwrapper;
|
|
|
|
|
|
|
|
public function __construct(TypeUnwrapper $typeUnwrapper)
|
|
|
|
{
|
|
|
|
$this->typeUnwrapper = $typeUnwrapper;
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:52:16 +00:00
|
|
|
public function getExpectedNameFromType(Type $type): ?string
|
|
|
|
{
|
2020-05-03 11:26:26 +00:00
|
|
|
if ($type instanceof UnionType) {
|
2020-07-19 18:19:25 +00:00
|
|
|
$type = $this->typeUnwrapper->unwrapNullableType($type);
|
2020-05-03 11:26:26 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = $this->getClassName($type);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return lcfirst($shortClassName);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return lcfirst($shortName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @source https://stackoverflow.com/a/2792045/1348344
|
|
|
|
*/
|
|
|
|
public function underscoreToName(string $underscoreName): string
|
|
|
|
{
|
2020-08-28 16:07:15 +00:00
|
|
|
$pascalCaseName = str_replace('_', '', ucwords($underscoreName, '_'));
|
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-08-05 20:45:36 +00:00
|
|
|
private function getClassName(TypeWithClassName $typeWithClassName): string
|
|
|
|
{
|
|
|
|
if ($typeWithClassName instanceof ShortenedObjectType) {
|
|
|
|
return $typeWithClassName->getFullyQualifiedName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $typeWithClassName->getClassName();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (Strings::endsWith($shortClassName, 'Interface')) {
|
|
|
|
$shortClassName = Strings::substring($shortClassName, 0, -strlen('Interface'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
if (! interface_exists($className)) {
|
|
|
|
return $shortName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// starts with "I\W+"?
|
|
|
|
if (Strings::match($shortName, '#^I[A-Z]#')) {
|
|
|
|
return Strings::substring($shortName, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Strings::match($shortName, '#Interface$#')) {
|
|
|
|
return Strings::substring($shortName, -strlen(self::INTERFACE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shortName;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctype_upper($shortClassName[1]) && ctype_lower($shortClassName[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isNumberOrUpper(string $char): bool
|
|
|
|
{
|
|
|
|
if (ctype_upper($char)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctype_digit($char);
|
2020-07-01 21:41:49 +00:00
|
|
|
}
|
2020-05-02 18:52:16 +00:00
|
|
|
}
|