implicit conversion of array to boolean.
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\HttpCache; |
||
15 | |||
16 | use GuzzleHttp\ClientInterface; |
||
17 | |||
18 | /** |
||
19 | * Purges Varnish. |
||
20 | * |
||
21 | * @author Kévin Dunglas <[email protected]> |
||
22 | * |
||
23 | * @experimental |
||
24 | */ |
||
25 | final class VarnishPurger implements PurgerInterface |
||
26 | { |
||
27 | private $clients; |
||
28 | |||
29 | /** |
||
30 | * @param ClientInterface[] $clients |
||
31 | */ |
||
32 | public function __construct(array $clients) |
||
33 | { |
||
34 | $this->clients = $clients; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function purge(array $iris) |
||
41 | { |
||
42 | if (!$iris) { |
||
0 ignored issues
–
show
|
|||
43 | return; |
||
44 | } |
||
45 | |||
46 | // Create the regex to purge all tags in just one request |
||
47 | $parts = array_map(function ($iri) { |
||
48 | return sprintf('(^|\,)%s($|\,)', preg_quote($iri)); |
||
49 | }, $iris); |
||
50 | |||
51 | $regex = \count($parts) > 1 ? sprintf('(%s)', implode(')|(', $parts)) : array_shift($parts); |
||
52 | |||
53 | foreach ($this->clients as $client) { |
||
54 | $client->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => $regex]]); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.