Completed
Pull Request — 1.x (#62)
by Akihito
02:38
created

CliHttpCache::getServerKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
final class CliHttpCache implements HttpCacheInterface
0 ignored issues
show
Deprecated Code introduced by
The interface BEAR\QueryRepository\HttpCacheInterface has been deprecated with message: Use \BEAR\Sunday\Extension\Transfer\HttpCacheInterface instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
8
{
9
    /**
10
     * @var ResourceStorageInterface
11
     */
12
    private $storage;
13
14 2
    public function __construct(ResourceStorageInterface $storage)
15
    {
16 2
        $this->storage = $storage;
17 2
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 2
    public function isNotModified(array $server) : bool
23
    {
24 2
        if (isset($server['argc']) && $server['argc'] === 4) {
25
            $server = $this->setRequestHeaders($server, $server['argv'][3]);
26
        }
27
28 2
        return isset($server['HTTP_IF_NONE_MATCH']) && $this->storage->hasEtag($server['HTTP_IF_NONE_MATCH']);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function transfer()
35
    {
36 1
        echo '304 Not Modified' . PHP_EOL . PHP_EOL;
37 1
    }
38
39
    private function setRequestHeaders(array $server, string $query) : array
40
    {
41
        \parse_str($query, $headers);
42
        foreach ($headers as $key => $header) {
0 ignored issues
show
Bug introduced by
The expression $headers of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
43
            $server[$this->getServerKey($key)] = (string) $header;
44
        }
45
46
        return $server;
47
    }
48
49
    private function getServerKey(string $key)
50
    {
51
        return sprintf('HTTP_%s', strtoupper(\str_replace('-', '_', $key)));
52
    }
53
}
54