Completed
Pull Request — develop (#1730)
by Mike
38:53 queued 29:03
created

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