1 | <?php |
||
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) |
|
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); |
|
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); |
|
74 | } |
||
75 | |||
76 | 2 | if ($cachedFile->getHash() !== $command->getFile()->md5()) { |
|
77 | 1 | return $this->updateCache($command, $next, $item); |
|
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) |
|
95 | |||
96 | /** |
||
97 | * Convert path to ItemName |
||
98 | * |
||
99 | * @param string $path |
||
100 | * @return string |
||
101 | */ |
||
102 | 5 | private function getItemName($path) |
|
106 | } |
||
107 |
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.