This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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
|
|||
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. ![]() |
|||
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);
![]() |
|||
208 | } |
||
209 | $this->menuManipulator->moveToPosition($menuItem, $item->getPosition()); |
||
210 | |||
211 | return $menuItem; |
||
212 | } |
||
213 | } |
||
214 |
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.