Conditions | 12 |
Paths | 42 |
Total Lines | 70 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
104 | protected function _alterComparisonExpression(Comparison $expression, $bundle, Query $query) |
||
105 | { |
||
106 | $field = $expression->getField(); |
||
107 | $column = is_string($field) ? $this->_toolbox->columnName($field) : ''; |
||
108 | |||
109 | if (empty($column) || |
||
110 | in_array($column, (array)$this->_table->schema()->columns()) || // ignore real columns |
||
111 | !in_array($column, $this->_toolbox->getAttributeNames()) || |
||
112 | !$this->_toolbox->isSearchable($column) // ignore no searchable virtual columns |
||
113 | ) { |
||
114 | // nothing to alter |
||
115 | return $expression; |
||
116 | } |
||
117 | |||
118 | $attr = $this->_toolbox->attributes($bundle)[$column]; |
||
119 | $value = $expression->getValue(); |
||
120 | $type = $this->_toolbox->getType($column); |
||
121 | $conjunction = $expression->getOperator(); |
||
122 | $conditions = [ |
||
123 | 'EavValues.eav_attribute_id' => $attr['id'], |
||
124 | "EavValues.value_{$type} {$conjunction}" => $value, |
||
125 | ]; |
||
126 | |||
127 | // subquery scope |
||
128 | $subQuery = TableRegistry::get('Eav.EavValues') |
||
129 | ->find() |
||
130 | ->select('EavValues.entity_id') |
||
131 | ->where($conditions); |
||
132 | |||
133 | // some variables |
||
134 | $conn = $query->connection(null); |
||
135 | list(, $driverClass) = namespaceSplit(strtolower(get_class($conn->driver()))); |
||
136 | $alias = $this->_table->alias(); |
||
137 | $pk = $this->_table->primaryKey(); |
||
138 | |||
139 | if (!is_array($pk)) { |
||
140 | $pk = [$pk]; |
||
141 | } |
||
142 | |||
143 | $pk = array_map(function ($key) use ($alias) { |
||
144 | return "{$alias}.{$key}"; |
||
145 | }, $pk); |
||
146 | |||
147 | switch ($driverClass) { |
||
148 | case 'sqlite': |
||
149 | $concat = implode(' || ', $pk); |
||
150 | $field = "({$concat} || '')"; |
||
151 | break; |
||
152 | case 'mysql': |
||
153 | case 'postgres': |
||
154 | case 'sqlserver': |
||
155 | default: |
||
156 | $concat = implode(', ', $pk); |
||
157 | $field = "CONCAT({$concat}, '')"; |
||
158 | break; |
||
159 | } |
||
160 | |||
161 | $ids = $subQuery->all()->extract('entity_id')->toArray(); |
||
162 | $ids = empty($ids) ? ['-1'] : $ids; |
||
163 | $expression->setField($field); |
||
164 | $expression->setValue($ids); |
||
165 | $expression->setOperator('IN'); |
||
166 | |||
167 | $class = new \ReflectionClass($expression); |
||
168 | $property = $class->getProperty('_type'); |
||
169 | $property->setAccessible(true); |
||
170 | $property->setValue($expression, 'string[]'); |
||
171 | |||
172 | return $expression; |
||
173 | } |
||
174 | } |
||
175 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.