Conditions | 4 |
Paths | 4 |
Total Lines | 87 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
89 | public function testFilterIsBetween(array $criteria): void |
||
90 | { |
||
91 | /** @var IsBetween&MockObject $filter */ |
||
92 | $filter = $this->getMockBuilder(IsBetween::class) |
||
93 | ->setConstructorArgs([$this->queryFilterManager, $this->typecaster]) |
||
94 | ->onlyMethods(['createParamName']) |
||
95 | ->getMock(); |
||
96 | |||
97 | $rootAlias = 'entity'; |
||
98 | $from = $criteria['from'] ?? ''; |
||
99 | $to = $criteria['to'] ?? ''; |
||
100 | $alias = $criteria['alias'] ?? 'entity'; |
||
101 | $fieldName = $criteria['field'] = $criteria['field'] ?? 'test'; |
||
102 | $formatType = $criteria['format'] ?? null; |
||
103 | |||
104 | $this->metadata->expects($this->once()) |
||
105 | ->method('hasField') |
||
106 | ->with($fieldName) |
||
107 | ->willReturn(true); |
||
108 | |||
109 | if (empty($criteria['alias'])) { |
||
110 | $alias = $rootAlias; |
||
111 | |||
112 | $this->queryBuilder->expects($this->once()) |
||
113 | ->method('getRootAlias') |
||
114 | ->willReturn($rootAlias); |
||
115 | } |
||
116 | |||
117 | /** @var Expr&MockObject $expr */ |
||
118 | $expr = $this->createMock(Expr::class); |
||
119 | |||
120 | $this->queryBuilder->expects($this->once()) |
||
121 | ->method('expr') |
||
122 | ->willReturn($expr); |
||
123 | |||
124 | $fromParam = $alias . 'abc123'; |
||
125 | $toParam = $alias . 'zyx999'; |
||
126 | |||
127 | $filter->expects($this->exactly(2)) |
||
128 | ->method('createParamName') |
||
129 | ->withConsecutive( |
||
130 | [$alias], |
||
131 | [$alias] |
||
132 | )->willReturnOnConsecutiveCalls( |
||
133 | $fromParam, |
||
134 | $toParam |
||
135 | ); |
||
136 | |||
137 | $isBetween = $alias . '.' . $fieldName . ' BETWEEN ' . $fromParam . ' AND ' . $toParam; |
||
138 | $expr->expects($this->once()) |
||
139 | ->method('between') |
||
140 | ->with( |
||
141 | $alias . '.' . $fieldName, |
||
142 | ':' . $fromParam, |
||
143 | ':' . $toParam |
||
144 | )->willReturn( |
||
145 | $isBetween |
||
146 | ); |
||
147 | |||
148 | if (empty($criteria['where']) || WhereType::AND === $criteria['where']) { |
||
149 | $this->queryBuilder->expects($this->once()) |
||
150 | ->method('andWhere') |
||
151 | ->with($isBetween); |
||
152 | } else { |
||
153 | $this->queryBuilder->expects($this->once()) |
||
154 | ->method('orWhere') |
||
155 | ->with($isBetween); |
||
156 | } |
||
157 | |||
158 | $this->typecaster->expects($this->exactly(2)) |
||
159 | ->method('typecast') |
||
160 | ->withConsecutive( |
||
161 | [$this->metadata, $fieldName, $from, $formatType, []], |
||
162 | [$this->metadata, $fieldName, $to, $formatType, []], |
||
163 | )->willReturnOnConsecutiveCalls( |
||
164 | $from, |
||
165 | $to |
||
166 | ); |
||
167 | |||
168 | $this->queryBuilder->expects($this->exactly(2)) |
||
169 | ->method('setParameter') |
||
170 | ->withConsecutive( |
||
171 | [$fromParam, $from], |
||
172 | [$toParam, $to] |
||
173 | ); |
||
174 | |||
175 | $filter->filter($this->queryBuilder, $this->metadata, $criteria); |
||
176 | } |
||
217 |