2020-10-31 13:59:40 +01:00
|
|
|
<?php
|
2021-05-02 13:50:34 +02:00
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-10-31 13:59:40 +01:00
|
|
|
namespace Rector\PSR4\FileInfoAnalyzer;
|
|
|
|
|
2021-05-23 09:33:26 +00:00
|
|
|
use RectorPrefix20210523\Nette\Utils\Strings;
|
2020-10-31 13:59:40 +01:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use Rector\CodingStyle\Naming\ClassNaming;
|
2021-04-13 02:12:48 +02:00
|
|
|
use Rector\Core\ValueObject\Application\File;
|
2020-10-31 13:59:40 +01:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
final class FileInfoDeletionAnalyzer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @see https://regex101.com/r/8BdrI3/1
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const TESTING_PREFIX_REGEX = '#input_(.*?)_#';
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2020-10-31 13:59:40 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\Naming\ClassNaming
|
2020-10-31 13:59:40 +01:00
|
|
|
*/
|
|
|
|
private $classNaming;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\CodingStyle\Naming\ClassNaming $classNaming)
|
2020-10-31 13:59:40 +01:00
|
|
|
{
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->classNaming = $classNaming;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isClassLikeAndFileInfoMatch(\Rector\Core\ValueObject\Application\File $file, \PhpParser\Node\Stmt\ClassLike $classLike) : bool
|
2020-10-31 13:59:40 +01:00
|
|
|
{
|
|
|
|
$className = $this->nodeNameResolver->getName($classLike);
|
|
|
|
if ($className === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-10-31 13:59:40 +01:00
|
|
|
}
|
2021-04-13 02:12:48 +02:00
|
|
|
$smartFileInfo = $file->getSmartFileInfo();
|
2020-10-31 13:59:40 +01:00
|
|
|
$baseFileName = $this->clearNameFromTestingPrefix($smartFileInfo->getBasenameWithoutSuffix());
|
|
|
|
$classShortName = $this->classNaming->getShortName($className);
|
|
|
|
return $baseFileName === $classShortName;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function clearNameFromTestingPrefix(string $name) : string
|
2020-10-31 13:59:40 +01:00
|
|
|
{
|
2021-05-23 09:33:26 +00:00
|
|
|
return \RectorPrefix20210523\Nette\Utils\Strings::replace($name, self::TESTING_PREFIX_REGEX, '');
|
2020-10-31 13:59:40 +01:00
|
|
|
}
|
|
|
|
}
|