Conditions | 17 |
Paths | 23 |
Total Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | public function addDefaultFields(array &$configuration, $entityClass, $adminName) |
||
29 | { |
||
30 | $mapping = [ |
||
31 | 'string' => [ |
||
32 | 'type' => 'string', |
||
33 | 'options' => [ |
||
34 | 'length' => 100, |
||
35 | ], |
||
36 | ], |
||
37 | 'boolean' => [ |
||
38 | 'type' => 'boolean', |
||
39 | 'options' => [], |
||
40 | ], |
||
41 | 'datetime' => [ |
||
42 | 'type' => 'date', |
||
43 | 'options' => [], |
||
44 | ], |
||
45 | ]; |
||
46 | |||
47 | foreach ($configuration['actions'] as $actionName => $action) { |
||
48 | if (null === $action) { |
||
49 | $action = []; |
||
50 | } |
||
51 | $metadata = $this->findMetadata($entityClass); |
||
52 | |||
53 | if (null === $metadata) { |
||
54 | continue; |
||
55 | } |
||
56 | |||
57 | // If fields are already defined, nothing to do |
||
58 | if (key_exists('fields', $action) && is_array($action['fields']) && count($action['fields'])) { |
||
59 | $fields = $action['fields']; |
||
60 | } else { |
||
61 | $fields = []; |
||
62 | |||
63 | // Get fields names from the metadata if no configuration is defined |
||
64 | foreach ($metadata->getFieldNames() as $fieldName) { |
||
65 | $fields[$fieldName] = null; |
||
66 | } |
||
67 | } |
||
68 | $actionField = $this->getActionField($metadata->getFieldNames()); |
||
69 | |||
70 | foreach ($fields as $fieldName => $fieldConfiguration) { |
||
71 | $fieldType = $metadata->getTypeOfField($fieldName); |
||
72 | |||
73 | if ( |
||
74 | 'list' === $actionName && |
||
75 | $fieldName === $actionField && |
||
76 | key_exists('edit', $configuration['actions']) |
||
77 | ) { |
||
78 | $fieldConfiguration = [ |
||
79 | 'type' => 'action', |
||
80 | 'options' => [ |
||
81 | 'admin' => $adminName, |
||
82 | 'action' => 'edit', |
||
83 | 'parameters' => [ |
||
84 | 'id' => null, |
||
85 | ], |
||
86 | ] |
||
87 | ]; |
||
88 | |||
89 | } else if ( |
||
90 | '_delete' === $fieldName && |
||
91 | !$metadata->hasField('_delete') && |
||
92 | null === $fieldConfiguration && |
||
93 | key_exists('delete', $configuration['actions']) |
||
94 | ) { |
||
95 | // If a "delete" field is declared, and if it is not configured in the metadata, and if no |
||
96 | // configuration is declared for this field, and if the "delete" action is allowed, we add a default |
||
97 | // "button" configuration |
||
98 | $fieldConfiguration = [ |
||
99 | 'type' => 'link', |
||
100 | 'options' => [ |
||
101 | 'admin' => $adminName, |
||
102 | 'action' => 'delete', |
||
103 | 'parameters' => [ |
||
104 | 'id' => null, |
||
105 | ], |
||
106 | 'text' => 'Delete', |
||
107 | 'class' => 'btn btn-sm btn-danger', |
||
108 | 'icon' => 'remove', |
||
109 | ], |
||
110 | ]; |
||
111 | |||
112 | } else if (key_exists($fieldType, $mapping)) { |
||
113 | $fieldConfiguration = $mapping[$metadata->getTypeOfField($fieldName)]; |
||
114 | } |
||
115 | $configuration['actions'][$actionName]['fields'][$fieldName] = $fieldConfiguration; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
268 |