Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Twig/Extension/PagePartTwigExtension.php (1 issue)

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\PagePartBundle\Twig\Extension;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
7
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
8
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
9
use Kunstmaan\PagePartBundle\Repository\PagePartRefRepository;
10
use Twig\Environment;
11
use Twig\Extension\AbstractExtension;
12
use Twig\TwigFunction;
13
14
/**
15
 * PagePartTwigExtension
16
 *
17
 * @final since 5.4
18
 */
19
class PagePartTwigExtension extends AbstractExtension
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    protected $em;
25
26
    public function __construct(EntityManager $em)
27
    {
28
        $this->em = $em;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function getFunctions()
35
    {
36
        return [
37
            new TwigFunction('render_pageparts', [$this, 'renderPageParts'], ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['html']]),
38
            new TwigFunction('getpageparts', ['needs_environment' => true, $this, 'getPageParts']),
39
            new TwigFunction('has_page_parts', [$this, 'hasPageParts']),
40
        ];
41
    }
42
43
    /**
44
     * @param array                 $twigContext The twig context
45
     * @param HasPagePartsInterface $page        The page
46
     * @param string                $contextName The pagepart context
47
     * @param array                 $parameters  Some extra parameters
48
     *
49
     * @return string
50
     */
51 View Code Duplication
    public function renderPageParts(Environment $env, array $twigContext, HasPagePartsInterface $page, $contextName = 'main', array $parameters = [])
52
    {
53
        $template = $env->load('@KunstmaanPagePart/PagePartTwigExtension/widget.html.twig');
54
        /* @var $entityRepository PagePartRefRepository */
55
        $pageparts = $this->getPageParts($page, $contextName);
56
        $newTwigContext = array_merge($parameters, [
57
            'pageparts' => $pageparts,
58
            'page' => $page,
59
        ]);
60
        $newTwigContext = array_merge($newTwigContext, $twigContext);
61
62
        return $template->render($newTwigContext);
63
    }
64
65
    /**
66
     * @param HasPagePartsInterface $page    The page
67
     * @param string                $context The pagepart context
68
     *
69
     * @return PagePartInterface[]
70
     */
71
    public function getPageParts(HasPagePartsInterface $page, $context = 'main')
72
    {
73
        /** @var $entityRepository PagePartRefRepository */
74
        $entityRepository = $this->em->getRepository(PagePartRef::class);
75
76
        return $entityRepository->getPageParts($page, $context);
77
    }
78
79
    /**
80
     * @param string $context
81
     *
82
     * @return bool
83
     */
84
    public function hasPageParts(HasPagePartsInterface $page, $context = 'main')
85
    {
86
        return $this->em->getRepository(PagePartRef::class)->hasPageParts($page, $context);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method hasPageParts() does only exist in the following implementations of said interface: Kunstmaan\PagePartBundle...y\PagePartRefRepository.

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87
    }
88
}
89