rector/rules/naming/src/Naming/PropertyNaming.php

229 lines
6.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\Naming\Naming;
use Nette\Utils\Strings;
2020-07-01 21:41:49 +00:00
use PHPStan\Type\ObjectType;
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;
use Rector\PHPStan\Type\SelfObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
2020-07-19 18:19:25 +00:00
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
2020-08-01 16:59:36 +00:00
/**
* @deprecated
* @todo merge with very similar logic in
* @see VariableNaming
*/
final class PropertyNaming
{
/**
* @var string[]
*/
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-07-19 18:19:25 +00:00
/**
* @var TypeUnwrapper
*/
private $typeUnwrapper;
public function __construct(TypeUnwrapper $typeUnwrapper)
{
$this->typeUnwrapper = $typeUnwrapper;
}
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
}
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
{
$pascalCaseName = str_replace('_', '', ucwords($underscoreName, '_'));
2020-07-01 21:41:49 +00:00
return lcfirst($pascalCaseName);
2020-07-01 21:41:49 +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;
}
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) {
if (ctype_upper($shortClassName[$i]) && $this->isNumberOrUpper($shortClassName[$i + 1])) {
$shortClassName[$i] = strtolower($shortClassName[$i]);
} else {
break;
}
}
return $shortClassName;
}
2020-05-03 11:26:26 +00:00
/**
* @param ObjectType|string $objectType
*/
private function resolveClassName($objectType): string
{
if ($objectType instanceof ObjectType) {
return $objectType->getClassName();
}
return $objectType;
}
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;
}
private function isPrefixedInterface(string $shortClassName): bool
2020-07-01 21:41:49 +00:00
{
if (strlen($shortClassName) <= 3) {
return false;
2020-07-01 21:41:49 +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
}
}