Completed
Push — develop ( 373768...b82813 )
by Mike
05:50
created

CacheMiddleware::getItemName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Parser\Middleware;
17
18
use phpDocumentor\Parser\Parser;
19
use phpDocumentor\Reflection\Middleware\Command;
20
use phpDocumentor\Reflection\Middleware\Middleware;
21
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
22
use phpDocumentor\Reflection\Php\File;
23
use Stash\Interfaces\PoolInterface;
24
use Stash\Item;
25
use Stash\Pool;
26
27
final class CacheMiddleware implements Middleware
28
{
29
    /**
30
     * Cache namespace used for this repository.
31
     */
32
    const CACHE_NAMESPACE = 'Documentation\\Api\\Php';
33
34
    /**
35
     * Cache pool used to store files.
36
     *
37
     * @var Pool
38
     */
39
    private $dataStore;
40
41
    /**
42
     * @var Parser
43
     */
44
    private $parser;
45
46 5
    public function __construct(PoolInterface $dataStore, Parser $parser)
47
    {
48 5
        $this->dataStore = $dataStore;
0 ignored issues
show
Documentation Bug introduced by Jaapio
$dataStore is of type object<Stash\Interfaces\PoolInterface>, but the property $dataStore was declared to be of type object<Stash\Pool>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49 5
        $this->parser = $parser;
50 5
    }
51
52
    /**
53
     * Executes this middle ware class.
54
     * A middle ware class MUST return a File object or call the $next callable.
55
     *
56
     * @param CreateCommand $command
57
     * @param callable $next
58
     *
59
     * @return File
60
     */
61 5
    public function execute(Command $command, callable $next)
62
    {
63 5
        $itemName = $this->getItemName($command->getFile()->path());
64 5
        $item = $this->dataStore->getItem($itemName);
65 5
        if ($item->isMiss() || $this->parser->isForced()) {
66 2
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by Jaapio
$command of type object<phpDocumentor\Ref...ion\Middleware\Command> is not a sub-type of object<phpDocumentor\Ref...ory\File\CreateCommand>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Middleware\Command to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by Jaapio
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67
        }
68
69
        /** @var File $cachedFile */
70 3
        $cachedFile = $item->get();
71
72 3
        if ($cachedFile === null) {
73 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by Jaapio
$command of type object<phpDocumentor\Ref...ion\Middleware\Command> is not a sub-type of object<phpDocumentor\Ref...ory\File\CreateCommand>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Middleware\Command to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by Jaapio
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74
        }
75
76 2
        if ($cachedFile->getHash() !== $command->getFile()->md5()) {
77 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by Jaapio
$command of type object<phpDocumentor\Ref...ion\Middleware\Command> is not a sub-type of object<phpDocumentor\Ref...ory\File\CreateCommand>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Middleware\Command to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by Jaapio
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
78
        }
79
80 1
        return $cachedFile;
81
    }
82
83
    /**
84
     * @param callable $next
85
     * @param Item $item
86
     * @return mixed
87
     */
88 4
    private function updateCache(CreateCommand $command, callable $next, $item)
89
    {
90 4
        $file = $next($command);
91 4
        $item->lock();
92 4
        $this->dataStore->save($item->set($file));
93 4
        return $file;
94
    }
95
96
    /**
97
     * Convert path to ItemName
98
     *
99
     * @param string $path
100
     * @return string
101
     */
102 5
    private function getItemName($path)
103
    {
104 5
        return static::CACHE_NAMESPACE . '\\' . md5($path);
105
    }
106
}
107