prepare config

This commit is contained in:
Tomas Votruba 2018-09-23 14:56:10 +02:00
parent 0efc746c93
commit 44f25ea3f1
3 changed files with 158 additions and 152 deletions

View File

@ -1,4 +1,128 @@
# source: https://book.cakephp.org/3.0/en/appendices/3-4-migration-guide.html # source: https://book.cakephp.org/3.0/en/appendices/3-4-migration-guide.html
services: 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('

View File

@ -102,40 +102,46 @@ CODE_SAMPLE
continue; continue;
} }
$newNames = $methodNamesToGetAndSetNames[$methodCallNode->name->toString()]; $config = $methodNamesToGetAndSetNames[$methodCallNode->name->toString()];
if ($newNames === null) { if ($config === null) {
$currentMethodName = $methodCallNode->name->toString(); $currentMethodName = $methodCallNode->name->toString();
return [ $config = [
'get' => 'get' . ucfirst($currentMethodName), 'get' => 'get' . ucfirst($currentMethodName),
'set' => 'set' . 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; 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: if (count($methodCallNode->args) >= $config['minimal_argument_count']) {
// - has arguments? => set return $config['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']; 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'];
} }
} }

View File

@ -2,137 +2,13 @@ services:
Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector: Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector:
$methodNamesByTypes: $methodNamesByTypes:
'Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector\Source\SomeModelType': '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: customMethod:
get: 'customMethodGetName' get: 'customMethodGetName'
set: 'customMethodSetName' set: 'customMethodSetName'
minimal_argument_count: 2
# config: ~ first_argument_type_to_set: 'array'
# - '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('