Completed
Pull Request — master (#952)
by Kévin
03:04
created

VarnishPurger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A purge() 0 15 4
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
namespace ApiPlatform\Core\HttpCache;
13
14
use GuzzleHttp\ClientInterface;
15
16
final class VarnishPurger implements PurgerInterface
17
{
18
    private $clients;
19
20
    /**
21
     * @param ClientInterface[] $clients
22
     */
23
    public function __construct(array $clients)
24
    {
25
        $this->clients = $clients;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function purge(array $iris)
32
    {
33
        if (!$iris) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $iris of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
34
            return;
35
        }
36
37
        $parts = array_map(function ($iri) {
38
            sprintf('(^|\,)%s($|\,)', preg_quote($iri));
39
        }, $iris);
40
        $regex = isset($parts[1]) ? sprintf('(%s)', implode(')|(', $parts)) : $parts[0];
41
42
        foreach ($this->clients as $client) {
43
            $client->sendAsync($client->request('BAN', '', ['headers' => ['X-Ban-Regex' => $regex]]));
0 ignored issues
show
Documentation introduced by
$client->request('BAN', ...Ban-Regex' => $regex))) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Psr\Http\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        }
45
    }
46
}
47