2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-05-01 11:15:18 +00:00
|
|
|
namespace Rector\CodingStyle\Naming;
|
|
|
|
|
2021-11-27 09:53:00 +00:00
|
|
|
use RectorPrefix20211127\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_;
|
2020-10-31 12:59:40 +00:00
|
|
|
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
2021-11-27 09:53:00 +00:00
|
|
|
use RectorPrefix20211127\Stringy\Stringy;
|
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
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|string $name
|
2020-05-24 08:46:34 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getVariableName($name) : string
|
2020-05-24 08:46:34 +00:00
|
|
|
{
|
|
|
|
$shortName = $this->getShortName($name);
|
2021-05-09 20:15:43 +00:00
|
|
|
return \lcfirst($shortName);
|
2020-05-24 08:46:34 +00:00
|
|
|
}
|
2019-05-25 23:15:21 +00:00
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\Stmt\ClassLike|string $name
|
2019-05-25 23:15:21 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getShortName($name) : string
|
2019-05-25 23:15:21 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($name instanceof \PhpParser\Node\Stmt\ClassLike) {
|
2020-12-21 02:12:42 +00:00
|
|
|
if ($name->name === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $this->getShortName($name->name);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($name instanceof \PhpParser\Node\Name || $name instanceof \PhpParser\Node\Identifier) {
|
2021-02-09 01:52:35 +00:00
|
|
|
$name = $name->toString();
|
2019-05-25 23:15:21 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$name = \trim($name, '\\');
|
2021-11-27 09:53:00 +00:00
|
|
|
return \RectorPrefix20211127\Nette\Utils\Strings::after($name, '\\', -1) ?: $name;
|
2019-05-01 11:15:18 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNamespace(string $fullyQualifiedName) : ?string
|
2019-05-03 14:14:53 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$fullyQualifiedName = \trim($fullyQualifiedName, '\\');
|
2021-11-27 09:53:00 +00:00
|
|
|
return \RectorPrefix20211127\Nette\Utils\Strings::before($fullyQualifiedName, '\\', -1) ?: null;
|
2019-05-03 14:14:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getNameFromFileInfo(\Symplify\SmartFileSystem\SmartFileInfo $smartFileInfo) : string
|
2020-05-12 15:20:40 +00:00
|
|
|
{
|
2020-07-19 18:52:42 +00:00
|
|
|
$basenameWithoutSuffix = $smartFileInfo->getBasenameWithoutSuffix();
|
2020-10-09 08:11:22 +00:00
|
|
|
// remove PHPUnit fixture file prefix
|
2021-05-10 22:23:08 +00:00
|
|
|
if (\Rector\Testing\PHPUnit\StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
2021-11-27 09:53:00 +00:00
|
|
|
$basenameWithoutSuffix = \RectorPrefix20211127\Nette\Utils\Strings::replace($basenameWithoutSuffix, self::INPUT_HASH_NAMING_REGEX, '');
|
2020-10-09 08:11:22 +00:00
|
|
|
}
|
2021-11-27 09:53:00 +00:00
|
|
|
$stringy = new \RectorPrefix20211127\Stringy\Stringy($basenameWithoutSuffix);
|
2021-03-17 19:59:18 +00:00
|
|
|
return (string) $stringy->upperCamelize();
|
2020-05-12 15:20:40 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* "some_function" → "someFunction"
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function createMethodNameFromFunction(\PhpParser\Node\Stmt\Function_ $function) : string
|
2020-05-12 15:20:40 +00:00
|
|
|
{
|
|
|
|
$functionName = (string) $function->name;
|
2021-11-27 09:53:00 +00:00
|
|
|
$stringy = new \RectorPrefix20211127\Stringy\Stringy($functionName);
|
2021-03-17 19:59:18 +00:00
|
|
|
return (string) $stringy->camelize();
|
2020-05-12 15:20:40 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function replaceSuffix(string $content, string $oldSuffix, string $newSuffix) : string
|
2021-02-13 13:25:47 +00:00
|
|
|
{
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\substr_compare($content, $oldSuffix, -\strlen($oldSuffix)) !== 0) {
|
2021-02-13 17:56:58 +00:00
|
|
|
return $content . $newSuffix;
|
2021-02-13 13:25:47 +00:00
|
|
|
}
|
2021-11-27 09:53:00 +00:00
|
|
|
$contentWithoutOldSuffix = \RectorPrefix20211127\Nette\Utils\Strings::substring($content, 0, -\RectorPrefix20211127\Nette\Utils\Strings::length($oldSuffix));
|
2021-02-13 17:56:58 +00:00
|
|
|
return $contentWithoutOldSuffix . $newSuffix;
|
2021-02-13 13:25:47 +00:00
|
|
|
}
|
2019-05-01 11:15:18 +00:00
|
|
|
}
|