Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc170eb

Browse files
committed
feature #27891 [Finder] Allow arrays as parameters of some methods for better fluent experience and code readability (jfredon)
This PR was squashed before being merged into the 4.2-dev branch (closes #27891). Discussion ---------- [Finder] Allow arrays as parameters of some methods for better fluent experience and code readability | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | symfony/symfony-docs#10038 Makes the Finder library a little more convenient to use by allowing the use of arrays as parameters of methods that can be called multiple times. This way of doing things was already present for the `Finder::in()` and `Finder::exclude()` methods, it has been extended to other methods that can be called several times to cumulate their effects. This allows a better use of fluent methods by avoiding breaking the chaining to iterate on array variables (a little more complexity in the Finder library for less complexity in applications that uses it). ```php // we could use $finder = Finder::create()->in($fileRepository)->name($fileList); // instead of $finder = Finder::create()->in($fileRepository); foreach ($fileList as $file) { $finder->name($file); } ``` In `.php_cs` files, this would make the code more readable by separating the configuration of the execution code: ```php <?php const RULES = [ '@symfony' => true, '@symfony:risky' => true, ]; const EXCLUDED_DIRS = [ // directories containing files with content that is autogenerated by `var_export`, which breaks CS in output code 'Symfony/Component/Cache/Tests/Marshaller/Fixtures', 'Symfony/Component/DependencyInjection/Tests/Fixtures', // ... ]; const EXCLUDED_FILES = [ // file content autogenerated by `var_export` 'Symfony/Component/Translation/Tests/fixtures/resources.php', // test template 'Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Custom/_name_entry_label.html.php', // ... ]; return PhpCsFixer\Config::create() ->setRules(RULES) ->setRiskyAllowed(true) ->setFinder( PhpCsFixer\Finder::create() ->in(__DIR__.'/src') ->append(array(__FILE__)) ->exclude(EXCLUDED_DIRS) ->notPath(EXCLUDED_FILES) ) ; ``` TODO - [x] complete the tests to validate the new syntax on all modified methods - [x] submit changes to the Finder documentation Commits ------- ad97cd7 [Finder] Allow arrays as parameters of some methods for better fluent experience and code readability
2 parents eb112a5 + ad97cd7 commit cc170eb

File tree

2 files changed

+162
-74
lines changed

2 files changed

+162
-74
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,20 @@ public function files()
107107
*
108108
* $finder->depth('> 1') // the Finder will start matching at level 1.
109109
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
110+
* $finder->depth(['>= 1', '< 3'])
110111
*
111-
* @param string|int $level The depth level expression
112+
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
112113
*
113114
* @return $this
114115
*
115116
* @see DepthRangeFilterIterator
116117
* @see NumberComparator
117118
*/
118-
public function depth($level)
119+
public function depth($levels)
119120
{
120-
$this->depths[] = new Comparator\NumberComparator($level);
121+
foreach ((array) $levels as $level) {
122+
$this->depths[] = new Comparator\NumberComparator($level);
123+
}
121124

122125
return $this;
123126
}
@@ -131,18 +134,21 @@ public function depth($level)
131134
* $finder->date('until 2 days ago');
132135
* $finder->date('> now - 2 hours');
133136
* $finder->date('>= 2005-10-15');
137+
* $finder->date(['>= 2005-10-15', '<= 2006-05-27']);
134138
*
135-
* @param string $date A date range string
139+
* @param string|string[] $dates A date range string or an array of date ranges
136140
*
137141
* @return $this
138142
*
139143
* @see strtotime
140144
* @see DateRangeFilterIterator
141145
* @see DateComparator
142146
*/
143-
public function date($date)
147+
public function date($dates)
144148
{
145-
$this->dates[] = new Comparator\DateComparator($date);
149+
foreach ((array) $dates as $date) {
150+
$this->dates[] = new Comparator\DateComparator($date);
151+
}
146152

147153
return $this;
148154
}
@@ -155,32 +161,33 @@ public function date($date)
155161
* $finder->name('*.php')
156162
* $finder->name('/\.php$/') // same as above
157163
* $finder->name('test.php')
164+
* $finder->name(['test.py', 'test.php'])
158165
*
159-
* @param string $pattern A pattern (a regexp, a glob, or a string)
166+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
160167
*
161168
* @return $this
162169
*
163170
* @see FilenameFilterIterator
164171
*/
165-
public function name($pattern)
172+
public function name($patterns)
166173
{
167-
$this->names[] = $pattern;
174+
$this->names = \array_merge($this->names, (array) $patterns);
168175

169176
return $this;
170177
}
171178

172179
/**
173180
* Adds rules that files must not match.
174181
*
175-
* @param string $pattern A pattern (a regexp, a glob, or a string)
182+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
176183
*
177184
* @return $this
178185
*
179186
* @see FilenameFilterIterator
180187
*/
181-
public function notName($pattern)
188+
public function notName($patterns)
182189
{
183-
$this->notNames[] = $pattern;
190+
$this->notNames = \array_merge($this->notNames, (array) $patterns);
184191

185192
return $this;
186193
}
@@ -192,16 +199,17 @@ public function notName($pattern)
192199
*
193200
* $finder->contains('Lorem ipsum')
194201
* $finder->contains('/Lorem ipsum/i')
202+
* $finder->contains(['dolor', '/ipsum/i'])
195203
*
196-
* @param string $pattern A pattern (string or regexp)
204+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
197205
*
198206
* @return $this
199207
*
200208
* @see FilecontentFilterIterator
201209
*/
202-
public function contains($pattern)
210+
public function contains($patterns)
203211
{
204-
$this->contains[] = $pattern;
212+
$this->contains = \array_merge($this->contains, (array) $patterns);
205213

206214
return $this;
207215
}
@@ -213,16 +221,17 @@ public function contains($pattern)
213221
*
214222
* $finder->notContains('Lorem ipsum')
215223
* $finder->notContains('/Lorem ipsum/i')
224+
* $finder->notContains(['lorem', '/dolor/i'])
216225
*
217-
* @param string $pattern A pattern (string or regexp)
226+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
218227
*
219228
* @return $this
220229
*
221230
* @see FilecontentFilterIterator
222231
*/
223-
public function notContains($pattern)
232+
public function notContains($patterns)
224233
{
225-
$this->notContains[] = $pattern;
234+
$this->notContains = \array_merge($this->notContains, (array) $patterns);
226235

227236
return $this;
228237
}
@@ -234,18 +243,19 @@ public function notContains($pattern)
234243
*
235244
* $finder->path('some/special/dir')
236245
* $finder->path('/some\/special\/dir/') // same as above
246+
* $finder->path(['some dir', 'another/dir'])
237247
*
238248
* Use only / as dirname separator.
239249
*
240-
* @param string $pattern A pattern (a regexp or a string)
250+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
241251
*
242252
* @return $this
243253
*
244254
* @see FilenameFilterIterator
245255
*/
246-
public function path($pattern)
256+
public function path($patterns)
247257
{
248-
$this->paths[] = $pattern;
258+
$this->paths = \array_merge($this->paths, (array) $patterns);
249259

250260
return $this;
251261
}
@@ -257,18 +267,19 @@ public function path($pattern)
257267
*
258268
* $finder->notPath('some/special/dir')
259269
* $finder->notPath('/some\/special\/dir/') // same as above
270+
* $finder->notPath(['some/file.txt', 'another/file.log'])
260271
*
261272
* Use only / as dirname separator.
262273
*
263-
* @param string $pattern A pattern (a regexp or a string)
274+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
264275
*
265276
* @return $this
266277
*
267278
* @see FilenameFilterIterator
268279
*/
269-
public function notPath($pattern)
280+
public function notPath($patterns)
270281
{
271-
$this->notPaths[] = $pattern;
282+
$this->notPaths = \array_merge($this->notPaths, (array) $patterns);
272283

273284
return $this;
274285
}
@@ -279,17 +290,20 @@ public function notPath($pattern)
279290
* $finder->size('> 10K');
280291
* $finder->size('<= 1Ki');
281292
* $finder->size(4);
293+
* $finder->size(['> 10K', '< 20K'])
282294
*
283-
* @param string|int $size A size range string or an integer
295+
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
284296
*
285297
* @return $this
286298
*
287299
* @see SizeRangeFilterIterator
288300
* @see NumberComparator
289301
*/
290-
public function size($size)
302+
public function size($sizes)
291303
{
292-
$this->sizes[] = new Comparator\NumberComparator($size);
304+
foreach ((array) $sizes as $size) {
305+
$this->sizes[] = new Comparator\NumberComparator($size);
306+
}
293307

294308
return $this;
295309
}

0 commit comments

Comments
 (0)