rector/rules/CodingStyle/Naming/ClassNaming.php

79 lines
3.0 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
2019-05-01 11:15:18 +00:00
namespace Rector\CodingStyle\Naming;
use RectorPrefix20211128\Nette\Utils\Strings;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Function_;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use RectorPrefix20211128\Stringy\Stringy;
use Symplify\SmartFileSystem\SmartFileInfo;
2019-05-01 11:15:18 +00:00
final class ClassNaming
{
/**
* @see https://regex101.com/r/8BdrI3/1
* @var string
*/
private const INPUT_HASH_NAMING_REGEX = '#input_(.*?)_#';
/**
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|string $name
*/
public function getVariableName($name) : string
{
$shortName = $this->getShortName($name);
return \lcfirst($shortName);
}
/**
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\Stmt\ClassLike|string $name
*/
public function getShortName($name) : string
{
if ($name instanceof \PhpParser\Node\Stmt\ClassLike) {
if ($name->name === null) {
return '';
}
return $this->getShortName($name->name);
}
if ($name instanceof \PhpParser\Node\Name || $name instanceof \PhpParser\Node\Identifier) {
$name = $name->toString();
}
$name = \trim($name, '\\');
return \RectorPrefix20211128\Nette\Utils\Strings::after($name, '\\', -1) ?: $name;
2019-05-01 11:15:18 +00:00
}
public function getNamespace(string $fullyQualifiedName) : ?string
{
$fullyQualifiedName = \trim($fullyQualifiedName, '\\');
return \RectorPrefix20211128\Nette\Utils\Strings::before($fullyQualifiedName, '\\', -1);
}
public function getNameFromFileInfo(\Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo) : string
{
2020-07-19 18:52:42 +00:00
$basenameWithoutSuffix = $smartFileInfo->getBasenameWithoutSuffix();
// remove PHPUnit fixture file prefix
if (\Rector\Testing\PHPUnit\StaticPHPUnitEnvironment::isPHPUnitRun()) {
$basenameWithoutSuffix = \RectorPrefix20211128\Nette\Utils\Strings::replace($basenameWithoutSuffix, self::INPUT_HASH_NAMING_REGEX, '');
}
$stringy = new \RectorPrefix20211128\Stringy\Stringy($basenameWithoutSuffix);
return (string) $stringy->upperCamelize();
}
/**
* "some_function" "someFunction"
*/
public function createMethodNameFromFunction(\PhpParser\Node\Stmt\Function_ $function) : string
{
$functionName = (string) $function->name;
$stringy = new \RectorPrefix20211128\Stringy\Stringy($functionName);
return (string) $stringy->camelize();
}
public function replaceSuffix(string $content, string $oldSuffix, string $newSuffix) : string
{
if (\substr_compare($content, $oldSuffix, -\strlen($oldSuffix)) !== 0) {
return $content . $newSuffix;
}
$contentWithoutOldSuffix = \RectorPrefix20211128\Nette\Utils\Strings::substring($content, 0, -\RectorPrefix20211128\Nette\Utils\Strings::length($oldSuffix));
return $contentWithoutOldSuffix . $newSuffix;
}
2019-05-01 11:15:18 +00:00
}