Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Parser/Middleware/CacheMiddleware.php (3 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of phpDocumentor.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright 2010-2018 Mike van Riel<[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Parser\Middleware;
15
16
use phpDocumentor\Parser\Parser;
17
use phpDocumentor\Reflection\Middleware\Middleware;
18
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
19
use phpDocumentor\Reflection\Php\File;
20
use Stash\Item;
21
use Stash\Pool;
22
23
final class CacheMiddleware implements Middleware
24
{
25
    /**
26
     * Cache namespace used for this repository.
27
     */
28
    const CACHE_NAMESPACE = 'Documentation\\Api\\Php';
29
30
    /**
31
     * Cache pool used to store files.
32
     *
33
     * @var Pool
34
     */
35
    private $dataStore;
36
37
    /**
38
     * @var Parser
39
     */
40
    private $parser;
41
42 2
    public function __construct(Pool $dataStore, Parser $parser)
43
    {
44 2
        $this->dataStore = $dataStore;
45 2
        $this->parser = $parser;
46 2
    }
47
48
    /**
49
     * Executes this middle ware class.
50
     * A middle ware class MUST return a File object or call the $next callable.
51
     *
52
     * @param CreateCommand $command
53
     * @param callable $next
54
     *
55
     * @return File
56
     */
57 2
    public function execute($command, callable $next)
58
    {
59 2
        $itemName = $this->getItemName($command->getFile()->path());
60 2
        $item = $this->dataStore->getItem($itemName);
61 2
        if ($item->isMiss() || $this->parser->isForced()) {
62 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
$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...
63
        }
64
65
        /** @var File $cachedFile */
66 1
        $cachedFile = $item->get();
67
68 1
        if ($cachedFile === null) {
69
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
$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...
70
        }
71
72 1
        if ($cachedFile->getHash() !== $command->getFile()->md5()) {
73 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
$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
        return $cachedFile;
77
    }
78
79
    /**
80
     * @param callable $next
81
     * @param Item $item
82
     * @return mixed
83
     */
84 2
    private function updateCache(CreateCommand $command, callable $next, $item)
85
    {
86 2
        $file = $next($command);
87 2
        $item->lock();
88 2
        $this->dataStore->save($item->set($file));
89 2
        return $file;
90
    }
91
92
    /**
93
     * Convert path to ItemName
94
     *
95
     * @param string $path
96
     * @return string
97
     */
98 2
    private function getItemName($path)
99
    {
100 2
        return static::CACHE_NAMESPACE . '\\' . md5($path);
101
    }
102
}
103