From 44f25ea3f1552b6a1de7e677c1a45e44223ac945 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 23 Sep 2018 14:56:10 +0200 Subject: [PATCH] prepare config --- config/level/cakephp/cakephp34.yml | 126 +++++++++++++++- .../Rector/MethodCall/ModalToGetSetRector.php | 46 +++--- .../MethodCall/ModalToGetSetRector/config.yml | 138 +----------------- 3 files changed, 158 insertions(+), 152 deletions(-) diff --git a/config/level/cakephp/cakephp34.yml b/config/level/cakephp/cakephp34.yml index 9ea32b4e5bf..3f9b3330fab 100644 --- a/config/level/cakephp/cakephp34.yml +++ b/config/level/cakephp/cakephp34.yml @@ -1,4 +1,128 @@ # source: https://book.cakephp.org/3.0/en/appendices/3-4-migration-guide.html services: - # ... + Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector: + $methodNamesByTypes: + Cake\Core\InstanceConfigTrait: + - 'config' + Cake\Core\StaticConfigTrait: + - 'config' + - 'dsnClassMap' + Cake\Console\ConsoleOptionParser: + - 'command' + - 'description' + - 'epilog' + Cake\Database\Connection: + - 'driver' + - 'schemaCollection' + #- '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(' \ No newline at end of file diff --git a/packages/CakePHP/src/Rector/MethodCall/ModalToGetSetRector.php b/packages/CakePHP/src/Rector/MethodCall/ModalToGetSetRector.php index 2fd026cbe91..f678de2346b 100644 --- a/packages/CakePHP/src/Rector/MethodCall/ModalToGetSetRector.php +++ b/packages/CakePHP/src/Rector/MethodCall/ModalToGetSetRector.php @@ -102,40 +102,46 @@ CODE_SAMPLE continue; } - $newNames = $methodNamesToGetAndSetNames[$methodCallNode->name->toString()]; - if ($newNames === null) { + $config = $methodNamesToGetAndSetNames[$methodCallNode->name->toString()]; + if ($config === null) { $currentMethodName = $methodCallNode->name->toString(); - return [ - 'get' => 'get' . ucfirst($currentMethodName), - 'set' => 'set' . ucfirst($currentMethodName) , + $config = [ + 'get' => 'get' . ucfirst($currentMethodName), + 'set' => 'set' . ucfirst($currentMethodName), ]; } - return $newNames; + // default minimal argument count for setter + $config['minimal_argument_count'] = $config['minimal_argument_count'] ?? 1; + + return $config; } return null; } /** - * @param mixed[] $typeAndMethodNames + * @param mixed[] $config */ - private function resolveNewMethodNameByCondition(MethodCall $methodCallNode, array $typeAndMethodNames): string + private function resolveNewMethodNameByCondition(MethodCall $methodCallNode, array $config): 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']; + if (count($methodCallNode->args) >= $config['minimal_argument_count']) { + return $config['set']; } - return $typeAndMethodNames['get']; + if (isset($methodCallNode->args[0])) { + // first argument type that is considered setter + if (isset($config['first_argument_type_to_set'])) { + $argumentType = $config['first_argument_type_to_set']; + $argumentValue = $methodCallNode->args[0]->value; + + if ($argumentType === 'array' && $argumentValue instanceof Array_) { + return $config['set']; + } + } + } + + return $config['get']; } } diff --git a/packages/CakePHP/tests/Rector/MethodCall/ModalToGetSetRector/config.yml b/packages/CakePHP/tests/Rector/MethodCall/ModalToGetSetRector/config.yml index b25c29bed87..7463a879c28 100644 --- a/packages/CakePHP/tests/Rector/MethodCall/ModalToGetSetRector/config.yml +++ b/packages/CakePHP/tests/Rector/MethodCall/ModalToGetSetRector/config.yml @@ -2,137 +2,13 @@ services: Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector: $methodNamesByTypes: 'Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector\Source\SomeModelType': - config: ~ + config: + get: 'getConfig' + set: 'setConfig' + minimal_argument_count: 2 + first_argument_type_to_set: 'array' 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(' \ No newline at end of file + minimal_argument_count: 2 + first_argument_type_to_set: 'array'