Issues (30)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Builder/MenuBuilder.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 Alpixel\Bundle\MenuBundle\Builder;
4
5
use Alpixel\Bundle\MenuBundle\Exception\LocaleException;
6
use Alpixel\Bundle\MenuBundle\Model\ItemInterface;
7
use Doctrine\ORM\EntityManager;
8
use Knp\Menu\FactoryInterface;
9
use Knp\Menu\MenuItem as KnpMenuItem;
10
use Knp\Menu\Util\MenuManipulator;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use UnexpectedValueException;
13
14
class MenuBuilder
15
{
16
    protected $currentUri;
17
    protected $entityManager;
18
    protected $factory;
19
    protected $knpMenu;
20
    protected $defaultLocale;
21
    protected $request;
22
    protected $menuManipulator;
23
24
    /**
25
     * MenuBuilder constructor.
26
     *
27
     * @param EntityManager    $entityManager
28
     * @param FactoryInterface $factory
29
     */
30
    public function __construct(MenuManipulator $menuManipulator, RequestStack $requestStack, EntityManager $entityManager, FactoryInterface $factory)
0 ignored issues
show
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
31
    {
32
        $this->menuManipulator = $menuManipulator;
33
        $this->entityManager = $entityManager;
34
        $this->factory = $factory;
35
36
        $request = $requestStack->getCurrentRequest();
37
        $this->request = $request;
38
        $this->currentUri = $request->getRequestUri();
39
    }
40
41
    /**
42
     * Check if locale is valid.
43
     *
44
     * @param $locale
45
     *
46
     * @return bool
47
     */
48
    public static function isValidLocale($locale)
49
    {
50
        if (is_string($locale) && !empty($locale)) {
51
            return true;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * The parameter $locale must be defined in your
59
     * symfony configuration file under parameters.
60
     *
61
     * @param $locale String
62
     *
63
     * @return $this
64
     */
65
    public function setDefaultLocale($locale)
66
    {
67
        if (self::isValidLocale($locale)) {
68
            $this->defaultLocale = $locale;
69
70
            return $this;
71
        }
72
73
        throw new LocaleException('
74
            The $locale parameter must be a non empty string or the locale is not defined
75
            under the Symfony parameters configuration.
76
        ');
77
    }
78
79
    /**
80
     * Return default locale.
81
     *
82
     * @return string
83
     */
84
    public function getDefaultLocale()
85
    {
86
        return $this->defaultLocale;
87
    }
88
89
    /**
90
     * Check if the machineName is valid.
91
     *
92
     * @param $machineName
93
     *
94
     * @return bool
95
     */
96
    public static function isValidMachineName($machineName)
97
    {
98
        if (is_string($machineName) && !empty($machineName)) {
99
            return true;
100
        }
101
102
        return false;
103
    }
104
105
    /**
106
     * Retrun null or a KnpMenuItem instance.
107
     *
108
     * @return null|KnpMenuItem
109
     */
110
    public function getKnpMenu()
111
    {
112
        return $this->knpMenu;
113
    }
114
115
    /**
116
     * Set the KnpMenuItem instance.
117
     *
118
     * @param KnpMenuItem $knpMenu
119
     *
120
     * @return $this
121
     */
122
    public function setKnpMenu(KnpMenuItem $knpMenu)
123
    {
124
        $this->knpMenu = $knpMenu;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Create KnpMenuItem.
131
     *
132
     * @param string $machineName The name of menu
133
     * @param string $locale      Language code (Recommanded ISO-639)
134
     *
135
     * @return KnpMenuItem Get formatted menu
136
     */
137
    public function createKnpMenu($machineName, $locale = null)
138
    {
139
        if (!self::isValidMachineName($machineName)) {
140
            throw new UnexpectedValueException('The parameter $machineName must be a non empty string');
141
        }
142
143
        if ($locale === null && $this->request !== null) {
144
            $locale = $this->request->getLocale();
145
        }
146
147
        if ($locale === null) {
148
            $locale = $this->getDefaultLocale();
149
        } elseif (!self::isValidLocale($locale)) {
150
            throw new LocaleException();
151
        }
152
153
        $this->setKnpMenu($this->factory->createItem('root'));
0 ignored issues
show
$this->factory->createItem('root') of type object<Knp\Menu\ItemInterface> is not a sub-type of object<Knp\Menu\MenuItem>. It seems like you assume a concrete implementation of the interface Knp\Menu\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...
154
155
        $menu = $this->entityManager
156
            ->getRepository('AlpixelMenuBundle:Menu')
157
            ->findOneMenuByMachineNameAndLocale($machineName, $locale);
158
159
        $items = $menu->getItems()->toArray();
160
161
        foreach ($items as $item) {
162
            if ($item->getParent() === null) {
163
                $this->getTree($this->knpMenu, $item);
164
            }
165
        }
166
167
        return $this->knpMenu;
168
    }
169
170
    /**
171
     * Create tree un KnpMenuItem.
172
     *
173
     * @param KnpMenuItem      $knpMenu
174
     * @param ItemInterface    $item
175
     * @param KnpMenuItem|null $parent
176
     *
177
     * @return KnpMenuItem A formatted KnpMenu
178
     */
179
    protected function getTree(KnpMenuItem $knpMenu, ItemInterface $item, KnpMenuItem $parent = null)
180
    {
181
        if ($parent === null) {
182
            $menuItem = $knpMenu->addChild($item);
183
        } else {
184
            $menuItem = $parent->addChild($item);
185
        }
186
187
        if (($uri = $item->getUri()) !== null) {
188
            if ($uri[0] == '/') {
189
                $baseUri = $this->request->getBasePath().
190
                    $this->request->getBaseURL().
191
                    $uri;
192
                $uri = $this->request->getSchemeAndHttpHost().$baseUri;
193
194
                if ($baseUri === $this->currentUri) {
195
                    $menuItem->setCurrent(true);
196
                }
197
            }
198
            $menuItem->setUri($uri);
199
        }
200
201
        $menuItem->setAttributes([
202
            'position' => $item->getPosition(),
203
            'slug'     => $item->getSlug(),
204
        ]);
205
206
        foreach ($item->getChildren() as $child) {
207
            $this->getTree($knpMenu, $child, $menuItem);
0 ignored issues
show
$menuItem is of type object<Knp\Menu\ItemInterface>, but the function expects a null|object<Knp\Menu\MenuItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
        }
209
        $this->menuManipulator->moveToPosition($menuItem, $item->getPosition());
210
211
        return $menuItem;
212
    }
213
}
214