Completed
Pull Request — master (#2751)
by Jeroen
06:09
created

AdminBundle/Toolbar/BundleVersionDataCollector.php (3 issues)

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
namespace Kunstmaan\AdminBundle\Toolbar;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
use Kunstmaan\AdminBundle\Helper\Toolbar\AbstractDataCollector;
7
use Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker;
8
use Symfony\Component\Cache\Adapter\AdapterInterface;
9
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class BundleVersionDataCollector extends AbstractDataCollector
14
{
15
    /** @var VersionChecker */
16
    private $versionChecker;
17
18
    /** @var AdapterInterface */
19
    private $cache;
20
21
    /**
22
     * @param VersionChecker                 $versionChecker
23
     * @param CacheProvider|AdapterInterface $cache
24
     */
25
    public function __construct(VersionChecker $versionChecker, /*Logger $logger,*/ /* AdapterInterface */ $cache)
26
    {
27
        $this->versionChecker = $versionChecker;
28
29
        if (!$cache instanceof CacheProvider && !$cache instanceof AdapterInterface) {
30
            // NEXT_MAJOR Add AdapterInterface typehint for the $cache parameter
31
            throw new \InvalidArgumentException(sprintf('The "$cache" parameter should extend from "%s" or implement "%s"', CacheProvider::class, AdapterInterface::class));
32
        }
33
34
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache can also be of type object<Doctrine\Common\Cache\CacheProvider>. However, the property $cache is declared as type object<Symfony\Component...apter\AdapterInterface>. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35 View Code Duplication
        if (\func_num_args() > 2) {
36
            @trigger_error(sprintf('Passing the "logger" service as the second argument in "%s" is deprecated in KunstmaanAdminBundle 5.1 and will be removed in KunstmaanAdminBundle 6.0. Remove the "logger" argument from your service definition.', __METHOD__), E_USER_DEPRECATED);
37
38
            $this->cache = func_get_arg(2);
39
        }
40
41
        if ($this->cache instanceof CacheProvider) {
42
            @trigger_error(sprintf('Passing an instance of "%s" as the second argument in "%s" is deprecated since KunstmaanAdminBundle 5.7 and an instance of "%s" will be required in KunstmaanAdminBundle 6.0.', CacheProvider::class, __METHOD__, AdapterInterface::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
44
            $this->cache = new DoctrineAdapter($cache);
0 ignored issues
show
It seems like $cache defined by parameter $cache on line 25 can also be of type object<Symfony\Component...apter\AdapterInterface>; however, Symfony\Component\Cache\...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\CacheProvider>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
        }
46
    }
47
48
    public function getAccessRoles()
49
    {
50
        return ['ROLE_SUPER_ADMIN'];
51
    }
52
53
    public function collectData()
54
    {
55
        $this->versionChecker->periodicallyCheck();
56
57
        $cacheItem = $this->cache->getItem(VersionChecker::CACHE_KEY);
58
        $collectorData = [];
59
        if ($cacheItem->isHit()) {
60
            $collectorData = $cacheItem->get() ?? [];
61
        }
62
63
        return [
64
            'data' => $collectorData,
65
        ];
66
    }
67
68
    public function collect(Request $request, Response $response, \Exception $exception = null)
69
    {
70
        if (!$this->isEnabled()) {
71
            $this->data = [];
72
        } else {
73
            $this->data = $this->collectData();
74
        }
75
    }
76
77
    /**
78
     * Gets the data for template
79
     *
80
     * @return array The request events
81
     */
82
    public function getTemplateData()
83
    {
84
        return $this->data;
85
    }
86
87
    public function getName()
88
    {
89
        return 'kuma_bundle_versions';
90
    }
91
92
    public function isEnabled()
93
    {
94
        return $this->versionChecker->isEnabled();
95
    }
96
97
    public function reset()
98
    {
99
        $this->data = [];
100
    }
101
}
102