rector/rules/CodingStyle/Naming/ClassNaming.php

96 lines
2.6 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 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 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 string|Name|Identifier $name
*/
public function getVariableName($name): string
{
$shortName = $this->getShortName($name);
return lcfirst($shortName);
}
/**
* @param string|Name|Identifier|ClassLike $name
*/
public function getShortName($name): string
{
if ($name instanceof ClassLike) {
if ($name->name === null) {
return '';
}
return $this->getShortName($name->name);
}
if ($name instanceof Name || $name instanceof Identifier) {
$name = $name->toString();
}
$name = trim($name, '\\');
return Strings::after($name, '\\', -1) ?: $name;
2019-05-01 11:15:18 +00:00
}
public function getNamespace(string $fullyQualifiedName): ?string
{
$fullyQualifiedName = trim($fullyQualifiedName, '\\');
return Strings::before($fullyQualifiedName, '\\', -1) ?: null;
}
public function getNameFromFileInfo(SmartFileInfo $smartFileInfo): string
{
2020-07-19 18:52:42 +00:00
$basenameWithoutSuffix = $smartFileInfo->getBasenameWithoutSuffix();
// remove PHPUnit fixture file prefix
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
$basenameWithoutSuffix = Strings::replace($basenameWithoutSuffix, self::INPUT_HASH_NAMING_REGEX, '');
}
$stringy = new Stringy($basenameWithoutSuffix);
return (string) $stringy->upperCamelize();
}
/**
* "some_function" "someFunction"
*/
public function createMethodNameFromFunction(Function_ $function): string
{
$functionName = (string) $function->name;
$stringy = new Stringy($functionName);
return (string) $stringy->camelize();
}
public function replaceSuffix(string $content, string $oldSuffix, string $newSuffix): string
{
if (! Strings::endsWith($content, $oldSuffix)) {
return $content . $newSuffix;
}
$contentWithoutOldSuffix = Strings::substring($content, 0, -Strings::length($oldSuffix));
return $contentWithoutOldSuffix . $newSuffix;
}
2019-05-01 11:15:18 +00:00
}