Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Kunstmaan/NodeBundle/Toolbar/NodeDataCollector.php (4 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\NodeBundle\Toolbar;
4
5
use Kunstmaan\AdminBundle\Helper\Toolbar\AbstractDataCollector;
6
use Kunstmaan\NodeBundle\Helper\NodeMenu;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
10
11
class NodeDataCollector extends AbstractDataCollector
12
{
13
    /**
14
     * @var NodeMenu
15
     */
16
    private $nodeMenu;
17
18
    /**
19
     * @var UrlGeneratorInterface
20
     */
21
    private $urlGenerator;
22
23
    /**
24
     * NodeDataCollector constructor.
25
     *
26
     * @param NodeMenu              $nodeMenu
27
     * @param UrlGeneratorInterface $urlGenerator
28
     */
29
    public function __construct(NodeMenu $nodeMenu, UrlGeneratorInterface $urlGenerator)
30
    {
31
        $this->nodeMenu = $nodeMenu;
32
        $this->urlGenerator = $urlGenerator;
33
    }
34
35
    /**
36
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
37
     */
38
    public function getAccessRoles()
39
    {
40
        return ['ROLE_ADMIN'];
41
    }
42
43
    /**
44
     * @return array|null
45
     */
46
    public function collectData()
47
    {
48
        $current = $this->nodeMenu->getCurrent();
49
50
        if ($current) {
51
            $id = $current->getNode()->getId();
52
53
            $route = $this->urlGenerator->generate('KunstmaanNodeBundle_nodes_edit', ['id' => $id]);
54
55
            $data = [
56
                'route' => $route,
57
                'nt' => $current->getNodeTranslation(),
58
            ];
59
60
            return ['data' => $data];
61
        }
62
63
        return [];
64
    }
65
66
    /**
67
     * @param Request         $request
68
     * @param Response        $response
69
     * @param \Exception|null $exception
70
     */
71 View Code Duplication
    public function collect(Request $request, Response $response, \Exception $exception = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if (!$this->showDataCollection($request, $response) || !$this->isEnabled()) {
74
            $this->data = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
        } else {
76
            $this->data = $this->collectData();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->collectData() can be null. However, the property $data is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
77
        }
78
    }
79
80
    /**
81
     * Gets the data for template
82
     *
83
     * @return array The request events
84
     */
85
    public function getTemplateData()
86
    {
87
        return $this->data;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getName()
94
    {
95
        return 'kuma_node';
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function isEnabled()
102
    {
103
        return true;
104
    }
105
106
    public function reset()
107
    {
108
        $this->data = [];
109
    }
110
}
111