init ParameterGuide

This commit is contained in:
Tomas Votruba 2018-09-26 14:54:12 +08:00
parent 3c3cfe85ff
commit 6d3c0be1eb
9 changed files with 124 additions and 2 deletions

View File

@ -41,6 +41,7 @@
"Rector\\Symfony\\": "packages/Symfony/src",
"Rector\\CakePHP\\": "packages/CakePHP/src",
"Rector\\Silverstripe\\": "packages/Silverstripe/src",
"Rector\\ParameterGuider\\": "packages/ParameterGuider/src",
"Rector\\Sensio\\": "packages/Sensio/src",
"Rector\\Sylius\\": "packages/Sylius/src",
"Rector\\PHPUnit\\": "packages/PHPUnit/src",

View File

@ -0,0 +1,7 @@
services:
_defaults:
autowire: true
public: true
Rector\ParameterGuider\:
resource: '../src'

View File

@ -0,0 +1,10 @@
<?php declare(strict_types=1);
namespace Rector\ParameterGuider\Exception;
use Exception;
final class ParameterTypoException extends Exception
{
}

View File

@ -0,0 +1,55 @@
<?php declare(strict_types=1);
namespace Rector\ParameterGuider;
use Nette\Utils\Strings;
use Rector\ParameterGuider\Exception\ParameterTypoException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
/**
* This class makes sure there are no typos in parameter names,
* and if so, it will suggest the correct parameter name.
*/
final class ParameterGuider
{
/**
* @var array
*/
private static $parametersWithMissplacedNames = [
// correct paremter => regex catching invalid values
'exclude_paths' => '#exclude(d)?_(path(s)?|dir(s)?|file(s)?)#'
];
/**
* @param mixed[] $parameters
*/
public function processParameters(ParameterBagInterface $parametersBag): void
{
$parameterNames = array_keys($parametersBag->all());
foreach ($parameterNames as $parameterName) {
foreach (self::$parametersWithMissplacedNames as $correctParameterName => $missplacedNamePattern) {
if ($parameterName === $correctParameterName) {
continue;
}
if (Strings::match($parameterName, $missplacedNamePattern)) {
$this->throwException($parameterName, $correctParameterName);
}
if (levenshtein($correctParameterName, $parameterName) < 2) {
$this->throwException($parameterName, $correctParameterName);
}
}
}
}
private function throwException(string $providedParameterName, string $correctParameterName): void
{
throw new ParameterTypoException(sprintf(
'Parameter "%s" does not exist. Use "%s" instead.',
$providedParameterName,
$correctParameterName
));
}
}

View File

@ -0,0 +1,18 @@
<?php declare(strict_types=1);
namespace Rector\ParameterGuider;
use PHPUnit\Framework\TestCase;
use Rector\DependencyInjection\ContainerFactory;
use Rector\ParameterGuider\Exception\ParameterTypoException;
final class ParameterGuiderTest extends TestCase
{
public function test(): void
{
// the validation is triggered on command run
$this->expectException(ParameterTypoException::class);
(new ContainerFactory())->createWithConfigFiles([__DIR__ . '/config.yml']);
}
}

View File

@ -0,0 +1,3 @@
parameters:
exclude_files:
- 'some_file'

View File

@ -3,10 +3,22 @@
namespace Rector\DependencyInjection;
use Psr\Container\ContainerInterface;
use Rector\ParameterGuider\ParameterGuider;
use function Safe\putenv;
use Symfony\Component\DependencyInjection\Container;
final class ContainerFactory
{
/**
* @var ParameterGuider
*/
private $parameterGuider;
public function __construct()
{
$this->parameterGuider = new ParameterGuider();
}
public function create(): ContainerInterface
{
$appKernel = new RectorKernel();
@ -14,7 +26,12 @@ final class ContainerFactory
// this is require to keep CLI verbosity independent on AppKernel dev/prod mode
putenv('SHELL_VERBOSITY=0');
return $appKernel->getContainer();
/** @var Container $container */
$container = $appKernel->getContainer();
$this->parameterGuider->processParameters($container->getParameterBag());
return $container;
}
/**
@ -27,6 +44,11 @@ final class ContainerFactory
// this is require to keep CLI verbosity independent on AppKernel dev/prod mode
putenv('SHELL_VERBOSITY=0');
return $appKernel->getContainer();
/** @var Container $container */
$container = $appKernel->getContainer();
$this->parameterGuider->processParameters($container->getParameterBag());
return $container;
}
}

View File

@ -1,5 +1,7 @@
imports:
- { resource: '../../packages/**/src/config/config.yml' }
# new config location
- { resource: '../../packages/**/config/config.yml' }
- { resource: 'services.yml' }
- { resource: 'external-services.yml' }

View File

@ -9,3 +9,7 @@ services:
# extra services
Rector\Symfony\Rector\Form\Helper\FormTypeStringToTypeProvider: ~
Rector\Console\Application: ~
Symfony\Component\Console\Application:
alias: 'Rector\Console\Application'