mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
allow custom get/set methods
This commit is contained in:
parent
31892ad485
commit
0efc746c93
@ -79,32 +79,63 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $methodCallNode): ?Node
|
||||
{
|
||||
if (! $this->isTypeAndMethodsMatch($methodCallNode)) {
|
||||
$typeAndMethodNames = $this->matchTypeAndMethodName($methodCallNode);
|
||||
if ($typeAndMethodNames === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$methodName = $methodCallNode->name->toString();
|
||||
|
||||
// @todo important, maybe unique condition
|
||||
if (count(
|
||||
$methodCallNode->args
|
||||
) >= 2 || isset($methodCallNode->args[0]) && $methodCallNode->args[0]->value instanceof Array_) {
|
||||
$methodCallNode->name = new Identifier('set' . ucfirst($methodName));
|
||||
} else {
|
||||
$methodCallNode->name = new Identifier('get' . ucfirst($methodName));
|
||||
}
|
||||
$newName = $this->resolveNewMethodNameByCondition($methodCallNode, $typeAndMethodNames);
|
||||
$methodCallNode->name = new Identifier($newName);
|
||||
|
||||
return $methodCallNode;
|
||||
}
|
||||
|
||||
private function isTypeAndMethodsMatch(MethodCall $methodCallNode): bool
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function matchTypeAndMethodName(MethodCall $methodCallNode): ?array
|
||||
{
|
||||
foreach ($this->methodNamesByTypes as $type => $methodNames) {
|
||||
if ($this->methodCallAnalyzer->isTypeAndMethods($methodCallNode, $type, $methodNames)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->methodNamesByTypes as $type => $methodNamesToGetAndSetNames) {
|
||||
$methodNames = array_keys($methodNamesToGetAndSetNames);
|
||||
if (! $this->methodCallAnalyzer->isTypeAndMethods($methodCallNode, $type, $methodNames)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
$newNames = $methodNamesToGetAndSetNames[$methodCallNode->name->toString()];
|
||||
if ($newNames === null) {
|
||||
$currentMethodName = $methodCallNode->name->toString();
|
||||
|
||||
return [
|
||||
'get' => 'get' . ucfirst($currentMethodName),
|
||||
'set' => 'set' . ucfirst($currentMethodName) ,
|
||||
];
|
||||
}
|
||||
|
||||
return $newNames;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $typeAndMethodNames
|
||||
*/
|
||||
private function resolveNewMethodNameByCondition(MethodCall $methodCallNode, array $typeAndMethodNames): string
|
||||
{
|
||||
// default:
|
||||
// - has arguments? => set
|
||||
// - get
|
||||
// "minimal_argument_count" : 2
|
||||
// "first_argument_type" : array
|
||||
|
||||
if (
|
||||
count($methodCallNode->args) >= 2 ||
|
||||
(isset($methodCallNode->args[0]) && $methodCallNode->args[0]->value instanceof Array_)
|
||||
) {
|
||||
return $typeAndMethodNames['set'];
|
||||
}
|
||||
|
||||
return $typeAndMethodNames['get'];
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector;
|
||||
|
||||
$object = new Source\SomeModelType;
|
||||
|
||||
$config = $object->customMethodGetName();
|
||||
$config = $object->customMethodGetName('key');
|
||||
|
||||
$object->customMethodSetName('key', 'value');
|
||||
$object->customMethodSetName(['key' => 'value']);
|
@ -21,6 +21,7 @@ final class ModalToGetSetRectorTest extends AbstractRectorTestCase
|
||||
public function provideWrongToFixedFiles(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
|
||||
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
|
@ -0,0 +1,11 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector;
|
||||
|
||||
$object = new Source\SomeModelType;
|
||||
|
||||
$config = $object->customMethod();
|
||||
$config = $object->customMethod('key');
|
||||
|
||||
$object->customMethod('key', 'value');
|
||||
$object->customMethod(['key' => 'value']);
|
@ -2,4 +2,137 @@ services:
|
||||
Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector:
|
||||
$methodNamesByTypes:
|
||||
'Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector\Source\SomeModelType':
|
||||
- 'config'
|
||||
config: ~
|
||||
customMethod:
|
||||
get: 'customMethodGetName'
|
||||
set: 'customMethodSetName'
|
||||
|
||||
# config: ~
|
||||
|
||||
# - 'useSavePoints' #() (now enableSavePoints()/isSavePointsEnabled('
|
||||
|
||||
# move to cakephp 3.6 set
|
||||
# Cake\Core\InstanceConfigTrait:
|
||||
# - 'config'
|
||||
# Cake\Core\StaticConfigTrait:
|
||||
# - 'config'
|
||||
# - 'dsnClassMap'
|
||||
# Cake\Console\ConsoleOptionParser:
|
||||
# - 'command'
|
||||
# - 'description'
|
||||
# - 'epilog'
|
||||
# Cake\Database\Connection:
|
||||
# - 'driver'
|
||||
# - 'schemaCollection'
|
||||
# # https://github.com/cakephp/cakephp/blob/4feee5463641e05c068b4d1d31dc5ee882b4240f/src/Database/Connection.php#L611-L640
|
||||
# - 'useSavePoints' #() (now enableSavePoints()/isSavePointsEnabled('
|
||||
# Cake\Database\Driver:
|
||||
# - 'autoQuoting' # (now enableAutoQuoting()/isAutoQuotingEnabled('
|
||||
# Cake\Database\Expression\FunctionExpression:
|
||||
# - 'name'
|
||||
# Cake\Database\Expression\QueryExpression:
|
||||
# - 'tieWith' #() (now setConjunction()/getConjunction('
|
||||
# Cake\Database\Expression\ValuesExpression:
|
||||
# - 'columns'
|
||||
# - 'values'
|
||||
# - 'query'
|
||||
# Cake\Database\Query:
|
||||
# - 'connection'
|
||||
# - 'selectTypeMap'
|
||||
# - 'bufferResults' #() (now enableBufferedResults()/isBufferedResultsEnabled('
|
||||
# Cake\Database\Schema\CachedCollection:
|
||||
# - 'cacheMetadata'
|
||||
# Cake\Database\Schema\TableSchema:
|
||||
# - 'options'
|
||||
# - 'temporary' #() (now setTemporary()/isTemporary('
|
||||
# Cake\Database\TypeMap:
|
||||
# - 'defaults'
|
||||
# - 'types'
|
||||
# Cake\Database\TypeMapTrait:
|
||||
# - 'typeMap'
|
||||
# - 'defaultTypes'
|
||||
# Cake\ORM\Association:
|
||||
# - 'name'
|
||||
# - 'cascadeCallbacks'
|
||||
# - 'source'
|
||||
# - 'target'
|
||||
# - 'conditions'
|
||||
# - 'bindingKey'
|
||||
# - 'foreignKey'
|
||||
# - 'dependent'
|
||||
# - 'joinType'
|
||||
# - 'property'
|
||||
# - 'strategy'
|
||||
# - 'finder'
|
||||
# Cake\ORM\Association\BelongsToMany:
|
||||
# - 'targetForeignKey'
|
||||
# - 'saveStrategy'
|
||||
# - 'conditions'
|
||||
# Cake\ORM\Association\HasMany:
|
||||
# - 'saveStrategy'
|
||||
# - 'foreignKey'
|
||||
# - 'sort'
|
||||
# Cake\ORM\Association\HasOne:
|
||||
# - 'foreignKey'
|
||||
# Cake\ORM\EagerLoadable:
|
||||
# - 'config'
|
||||
# #- 'setter part of canBeJoined() (now setCanBeJoined('
|
||||
# Cake\ORM\EagerLoader:
|
||||
# - 'matching' #() (getMatching() will have to be called after setMatching() to keep the old behavio'
|
||||
# - 'autoFields' #() (now enableAutoFields()/isAutoFieldsEnabled('
|
||||
# Cake\ORM\Locator\TableLocator:
|
||||
# - 'config'
|
||||
# Cake\ORM\Query:
|
||||
# - 'eagerLoader'
|
||||
# - 'hydrate' #() (now enableHydration()/isHydrationEnabled('
|
||||
# - 'autoFields' #() (now enableAutoFields()/isAutoFieldsEnabled('
|
||||
# Cake\ORM\Table:
|
||||
# - 'table'
|
||||
# - 'alias'
|
||||
# - 'registryAlias'
|
||||
# - 'connection'
|
||||
# - 'schema'
|
||||
# - 'primaryKey'
|
||||
# - 'displayField'
|
||||
# - 'entityClass'
|
||||
# Cake\Mailer\Email:
|
||||
# - 'from'
|
||||
# - 'sender'
|
||||
# - 'replyTo'
|
||||
# - 'readReceipt'
|
||||
# - 'returnPath'
|
||||
# - 'to'
|
||||
# - 'cc'
|
||||
# - 'bcc'
|
||||
# - 'charset'
|
||||
# - 'headerCharset'
|
||||
# - 'emailPattern'
|
||||
# - 'subject'
|
||||
# - 'template() (now setTemplate()/getTemplate() and setLayout()/getLayout('
|
||||
# - 'viewRender() (now setViewRenderer()/getViewRenderer('
|
||||
# - 'viewVars'
|
||||
# - 'theme'
|
||||
# - 'helpers'
|
||||
# - 'emailFormat'
|
||||
# - 'transport'
|
||||
# - 'messageId'
|
||||
# - 'domain'
|
||||
# - 'attachments'
|
||||
# - 'configTransport'
|
||||
# - 'profile'
|
||||
# Cake\Validation\Validator:
|
||||
# - 'provider'
|
||||
# Cake\View\StringTemplateTrait:
|
||||
# - 'templates'
|
||||
# Cake\View\ViewBuilder:
|
||||
# - 'templatePath'
|
||||
# - 'layoutPath'
|
||||
# - 'plugin'
|
||||
# - 'helpers'
|
||||
# - 'theme'
|
||||
# - 'template'
|
||||
# - 'layout'
|
||||
# - 'options'
|
||||
# - 'name'
|
||||
# - 'className'
|
||||
# - 'autoLayout' #() (now enableAutoLayout()/isAutoLayoutEnabled('
|
Loading…
x
Reference in New Issue
Block a user