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\FormBundle\Helper; |
||
4 | |||
5 | use ArrayObject; |
||
6 | use Doctrine\ORM\EntityManager; |
||
7 | use Kunstmaan\FormBundle\Entity\FormAdaptorInterface; |
||
8 | use Kunstmaan\FormBundle\Entity\FormSubmission; |
||
9 | use Kunstmaan\FormBundle\Entity\FormSubmissionField; |
||
10 | use Kunstmaan\FormBundle\Event\FormEvents; |
||
11 | use Kunstmaan\FormBundle\Event\SubmissionEvent; |
||
12 | use Kunstmaan\NodeBundle\Helper\RenderContext; |
||
13 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
14 | use Symfony\Component\Form\Extension\Core\Type\FormType; |
||
15 | use Symfony\Component\Form\FormBuilderInterface; |
||
16 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | use Symfony\Component\Routing\RouterInterface; |
||
19 | |||
20 | /** |
||
21 | * The form handler handles everything from creating the form to handling the submitted form |
||
22 | */ |
||
23 | class FormHandler implements FormHandlerInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var ContainerInterface |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | public function __construct(ContainerInterface $container) |
||
31 | { |
||
32 | $this->container = $container; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param FormPageInterface $page The form page |
||
37 | * @param Request $request The request |
||
38 | * @param RenderContext $context The render context |
||
39 | * |
||
40 | * @return RedirectResponse|void|null |
||
41 | */ |
||
42 | public function handleForm(FormPageInterface $page, Request $request, RenderContext $context) |
||
43 | { |
||
44 | /* @var EntityManager $em */ |
||
45 | $em = $this->container->get('doctrine.orm.entity_manager'); |
||
46 | /* @var FormBuilderInterface $formBuilder */ |
||
47 | $formBuilder = $this->container->get('form.factory')->createBuilder(FormType::class); |
||
48 | /* @var RouterInterface $router */ |
||
49 | $router = $this->container->get('router'); |
||
50 | /* @var ArrayObject $fields */ |
||
51 | $fields = new ArrayObject(); |
||
52 | $pageParts = $em->getRepository('KunstmaanPagePartBundle:PagePartRef')->getPageParts($page, $page->getFormElementsContext()); |
||
0 ignored issues
–
show
|
|||
53 | foreach ($pageParts as $sequence => $pagePart) { |
||
54 | if ($pagePart instanceof FormAdaptorInterface) { |
||
55 | $pagePart->adaptForm($formBuilder, $fields, $sequence); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | $form = $formBuilder->getForm(); |
||
60 | if ($request->getMethod() == 'POST') { |
||
61 | $form->handleRequest($request); |
||
62 | if ($form->isSubmitted() && $form->isValid()) { |
||
63 | $formSubmission = new FormSubmission(); |
||
64 | $formSubmission->setIpAddress($request->getClientIp()); |
||
65 | $formSubmission->setNode($em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page)); |
||
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Doctrine\Persistence\ObjectRepository as the method getNodeFor() does only exist in the following implementations of said interface: Kunstmaan\NodeBundle\Repository\NodeRepository .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
66 | $formSubmission->setLang($request->getLocale()); |
||
67 | $em->persist($formSubmission); |
||
68 | |||
69 | /* @var FormSubmissionField $field */ |
||
70 | foreach ($fields as $field) { |
||
71 | $field->setSubmission($formSubmission); |
||
72 | $field->onValidPost($form, $formBuilder, $request, $this->container); |
||
73 | $em->persist($field); |
||
74 | } |
||
75 | |||
76 | $em->flush(); |
||
77 | $em->refresh($formSubmission); |
||
78 | |||
79 | $event = new SubmissionEvent($formSubmission, $page); |
||
80 | $this->container->get('event_dispatcher')->dispatch(FormEvents::ADD_SUBMISSION, $event); |
||
81 | |||
82 | return new RedirectResponse($page->generateThankYouUrl($router, $context)); |
||
83 | } |
||
84 | } |
||
85 | $context['frontendform'] = $form->createView(); |
||
86 | $context['frontendformobject'] = $form; |
||
87 | |||
88 | return null; |
||
89 | } |
||
90 | } |
||
91 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: