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;
|
2019-11-14 16:48:46 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2019-05-25 23:15:21 +00:00
|
|
|
use PhpParser\Node\Name;
|
2020-12-21 02:12:42 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2020-05-12 15:20:40 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use Rector\Core\Util\StaticRectorStrings;
|
2020-10-31 12:59:40 +00:00
|
|
|
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
2020-05-12 15:20:40 +00:00
|
|
|
use Symplify\SmartFileSystem\SmartFileInfo;
|
2019-05-01 11:15:18 +00:00
|
|
|
|
|
|
|
final class ClassNaming
|
|
|
|
{
|
2020-10-09 08:11:22 +00:00
|
|
|
/**
|
|
|
|
* @see https://regex101.com/r/8BdrI3/1
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const INPUT_HASH_NAMING_REGEX = '#input_(.*?)_#';
|
|
|
|
|
2020-05-24 08:46:34 +00:00
|
|
|
/**
|
|
|
|
* @param string|Name|Identifier $name
|
|
|
|
*/
|
|
|
|
public function getVariableName($name): string
|
|
|
|
{
|
|
|
|
$shortName = $this->getShortName($name);
|
|
|
|
return lcfirst($shortName);
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:15:21 +00:00
|
|
|
/**
|
2020-12-21 02:12:42 +00:00
|
|
|
* @param string|Name|Identifier|ClassLike $name
|
2019-05-25 23:15:21 +00:00
|
|
|
*/
|
|
|
|
public function getShortName($name): string
|
|
|
|
{
|
2020-12-21 02:12:42 +00:00
|
|
|
if ($name instanceof ClassLike) {
|
|
|
|
if ($name->name === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getShortName($name->name);
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:48:46 +00:00
|
|
|
if ($name instanceof Name || $name instanceof Identifier) {
|
2021-02-09 01:52:35 +00:00
|
|
|
$name = $name->toString();
|
2019-05-25 23:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$name = trim($name, '\\');
|
2019-05-03 14:14:53 +00:00
|
|
|
|
2019-05-25 23:15:21 +00:00
|
|
|
return Strings::after($name, '\\', -1) ?: $name;
|
2019-05-01 11:15:18 +00:00
|
|
|
}
|
2019-05-03 14:14:53 +00:00
|
|
|
|
|
|
|
public function getNamespace(string $fullyQualifiedName): ?string
|
|
|
|
{
|
|
|
|
$fullyQualifiedName = trim($fullyQualifiedName, '\\');
|
|
|
|
|
|
|
|
return Strings::before($fullyQualifiedName, '\\', -1) ?: null;
|
|
|
|
}
|
2020-05-12 15:20:40 +00:00
|
|
|
|
|
|
|
public function getNameFromFileInfo(SmartFileInfo $smartFileInfo): string
|
|
|
|
{
|
2020-07-19 18:52:42 +00:00
|
|
|
$basenameWithoutSuffix = $smartFileInfo->getBasenameWithoutSuffix();
|
2020-05-12 15:20:40 +00:00
|
|
|
|
2020-10-09 08:11:22 +00:00
|
|
|
// remove PHPUnit fixture file prefix
|
|
|
|
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
2020-12-01 13:52:24 +00:00
|
|
|
$basenameWithoutSuffix = Strings::replace($basenameWithoutSuffix, self::INPUT_HASH_NAMING_REGEX, '');
|
2020-10-09 08:11:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:07:15 +00:00
|
|
|
return StaticRectorStrings::underscoreToPascalCase($basenameWithoutSuffix);
|
2020-05-12 15:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "some_function" → "someFunction"
|
|
|
|
*/
|
|
|
|
public function createMethodNameFromFunction(Function_ $function): string
|
|
|
|
{
|
|
|
|
$functionName = (string) $function->name;
|
2020-08-28 16:07:15 +00:00
|
|
|
return StaticRectorStrings::underscoreToCamelCase($functionName);
|
2020-05-12 15:20:40 +00:00
|
|
|
}
|
2021-02-13 13:25:47 +00:00
|
|
|
|
2021-02-13 17:56:58 +00:00
|
|
|
public function replaceSuffix(string $content, string $oldSuffix, string $newSuffix): string
|
2021-02-13 13:25:47 +00:00
|
|
|
{
|
2021-02-13 17:56:58 +00:00
|
|
|
if (! Strings::endsWith($content, $oldSuffix)) {
|
|
|
|
return $content . $newSuffix;
|
2021-02-13 13:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:56:58 +00:00
|
|
|
$contentWithoutOldSuffix = Strings::substring($content, 0, -Strings::length($oldSuffix));
|
|
|
|
return $contentWithoutOldSuffix . $newSuffix;
|
2021-02-13 13:25:47 +00:00
|
|
|
}
|
2019-05-01 11:15:18 +00:00
|
|
|
}
|