mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 11:33:33 +01:00
add FormEvents rector to symfony30.yml
This commit is contained in:
parent
647dbd0cc6
commit
bfb6e4f93c
14
README.md
14
README.md
@ -144,6 +144,20 @@ You can:
|
||||
'renderFormBegin': ['Nette\Bridges\FormsLatte\Runtime', 'renderFormBegin']
|
||||
```
|
||||
|
||||
- **change class constant name**
|
||||
|
||||
```yml
|
||||
# symfony30.yml
|
||||
rectors:
|
||||
Rector\Rector\Dynamic\ClassConstantReplacerRector:
|
||||
# class:
|
||||
# OLD_CONSTANT: NEW_CONSTANT
|
||||
'Symfony\Component\Form\FormEvents':
|
||||
'PRE_BIND': 'PRE_SUBMIT'
|
||||
'BIND': 'SUBMIT'
|
||||
'POST_BIND': 'POST_SUBMIT'
|
||||
```
|
||||
|
||||
- or **replace underscore naming `_` with namespaces `\`**
|
||||
|
||||
```yml
|
||||
|
@ -12,34 +12,14 @@ final class ClassConstAnalyzer
|
||||
/**
|
||||
* @param string[] $constantNames
|
||||
*/
|
||||
public function isClassConstFetchOfClassAndConstantNames(Node $node, string $class, array $constantNames): bool
|
||||
public function isTypeAndNames(Node $node, string $type, array $constantNames): bool
|
||||
{
|
||||
if (! $node instanceof ClassConstFetch) {
|
||||
if (! $this->isType($node, $type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->isClassName($node, $class)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isConstantName($node, $constantNames);
|
||||
}
|
||||
|
||||
private function isClassName(ClassConstFetch $classConstFetchNode, string $className): bool
|
||||
{
|
||||
$nodeClass = $this->resolveClass($classConstFetchNode);
|
||||
|
||||
return $nodeClass === $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $constantNames
|
||||
*/
|
||||
private function isConstantName(ClassConstFetch $node, array $constantNames): bool
|
||||
{
|
||||
$nodeConstantName = $node->name->name;
|
||||
|
||||
return in_array($nodeConstantName, $constantNames, true);
|
||||
/** @var ClassConstFetch $node */
|
||||
return $this->isNames($node, $constantNames);
|
||||
}
|
||||
|
||||
public function matchTypes(Node $node, array $types): ?string
|
||||
@ -48,12 +28,33 @@ final class ClassConstAnalyzer
|
||||
return null;
|
||||
}
|
||||
|
||||
$class = $this->resolveClass($node);
|
||||
$class = $this->resolveType($node);
|
||||
|
||||
return in_array($class, $types, true) ? $class : null;
|
||||
}
|
||||
|
||||
private function resolveClass(ClassConstFetch $classConstFetchNode): string
|
||||
private function isType(Node $node, string $type): bool
|
||||
{
|
||||
if (! $node instanceof ClassConstFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$nodeClass = $this->resolveType($node);
|
||||
|
||||
return $nodeClass === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $names
|
||||
*/
|
||||
private function isNames(ClassConstFetch $node, array $names): bool
|
||||
{
|
||||
$nodeConstantName = $node->name->name;
|
||||
|
||||
return in_array($nodeConstantName, $names, true);
|
||||
}
|
||||
|
||||
private function resolveType(ClassConstFetch $classConstFetchNode): string
|
||||
{
|
||||
$classFullyQualifiedName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
|
@ -48,7 +48,7 @@ final class FormNegativeRulesRector extends AbstractRector
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->classConstAnalyzer->isClassConstFetchOfClassAndConstantNames(
|
||||
return $this->classConstAnalyzer->isTypeAndNames(
|
||||
$node->expr,
|
||||
self::FORM_CLASS,
|
||||
self::RULE_NAMES
|
||||
|
@ -47,7 +47,7 @@ final class ConsoleExceptionToErrorEventConstantRector extends AbstractRector
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if ($this->classConstAnalyzer->isClassConstFetchOfClassAndConstantNames(
|
||||
if ($this->classConstAnalyzer->isTypeAndNames(
|
||||
$node,
|
||||
self::CONSOLE_EVENTS_CLASS,
|
||||
['EXCEPTION']
|
||||
|
@ -5,8 +5,6 @@ namespace Rector\Rector\Dynamic;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\NodeAnalyzer\ClassConstAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
@ -48,6 +46,7 @@ final class ClassConstantReplacerRector extends AbstractRector
|
||||
$matchedType = $this->classConstAnalyzer->matchTypes($node, $this->getTypes());
|
||||
if ($matchedType) {
|
||||
$this->activeType = $matchedType;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
rectors:
|
||||
# @todo dynamic constant rename rector
|
||||
Rector\Rector\Dynamic\ClassConstantReplacerRector:
|
||||
# form
|
||||
'Symfony\Component\Form\FormEvents':
|
||||
'PRE_BIND': 'PRE_SUBMIT'
|
||||
'BIND': 'SUBMIT'
|
||||
'POST_BIND': 'POST_SUBMIT'
|
||||
|
||||
Rector\Rector\Dynamic\MethodNameReplacerRector:
|
||||
# form
|
||||
|
Loading…
x
Reference in New Issue
Block a user