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\NodeSearchBundle\Twig; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
||
7 | use Kunstmaan\NodeBundle\Entity\Node; |
||
8 | use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService; |
||
9 | use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface; |
||
10 | use Twig\Environment; |
||
11 | use Twig\Extension\AbstractExtension; |
||
12 | use Twig\TwigFunction; |
||
13 | |||
14 | /** |
||
15 | * @final since 5.4 |
||
16 | */ |
||
17 | class KunstmaanNodeSearchTwigExtension extends AbstractExtension |
||
18 | { |
||
19 | /** |
||
20 | * @var EntityManager |
||
21 | */ |
||
22 | private $em; |
||
23 | |||
24 | /** |
||
25 | * @var IndexablePagePartsService |
||
26 | */ |
||
27 | private $indexablePagePartsService; |
||
28 | |||
29 | /** |
||
30 | * @param EntityManager $em |
||
31 | * @param IndexablePagePartsService $indexablePagePartsService |
||
32 | */ |
||
33 | public function __construct(EntityManager $em, IndexablePagePartsService $indexablePagePartsService) |
||
34 | { |
||
35 | $this->em = $em; |
||
36 | $this->indexablePagePartsService = $indexablePagePartsService; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Returns a list of functions to add to the existing list. |
||
41 | * |
||
42 | * @return array An array of functions |
||
43 | */ |
||
44 | public function getFunctions() |
||
45 | { |
||
46 | return array( |
||
47 | new TwigFunction('get_parent_page', array($this, 'getParentPage')), |
||
48 | new TwigFunction('render_indexable_pageparts', array($this, 'renderIndexablePageParts'), array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('html'))), |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param HasNodeInterface $page |
||
54 | * @param string $locale |
||
55 | * |
||
56 | * @return HasNodeInterface |
||
57 | */ |
||
58 | public function getParentPage(HasNodeInterface $page, $locale) |
||
59 | { |
||
60 | /** @var Node $node */ |
||
61 | $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page); |
||
0 ignored issues
–
show
|
|||
62 | $parentNode = $node->getParent(); |
||
63 | $nodeTranslation = $parentNode->getNodeTranslation($locale); |
||
64 | |||
65 | return $nodeTranslation->getRef($this->em); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param Environment $env |
||
70 | * @param array $twigContext The twig context |
||
71 | * @param HasPagePartsInterface $page The page |
||
72 | * @param string $contextName The pagepart context |
||
73 | * @param array $parameters Some extra parameters |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | View Code Duplication | public function renderIndexablePageParts( |
|
78 | Environment $env, |
||
79 | array $twigContext, |
||
80 | HasPagePartsInterface $page, |
||
81 | $contextName = 'main', |
||
82 | array $parameters = array() |
||
83 | ) { |
||
84 | $template = $env->load('@KunstmaanNodeSearch/PagePart/view.html.twig'); |
||
85 | $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page, $contextName); |
||
86 | $newTwigContext = array_merge( |
||
87 | $parameters, |
||
88 | array( |
||
89 | 'pageparts' => $pageparts, |
||
90 | ) |
||
91 | ); |
||
92 | $newTwigContext = array_merge($newTwigContext, $twigContext); |
||
93 | |||
94 | return $template->render($newTwigContext); |
||
95 | } |
||
96 | } |
||
97 |
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: