Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

buildReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessPageBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Metadata\ClassMetadata;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
9
/**
10
 * VirtualBusinessPageReferenceBuilder.
11
 */
12
class VirtualBusinessPageReferenceBuilder extends BusinessPageReferenceBuilder
13
{
14
    public function buildReference(View $businessPage, EntityManager $em)
15
    {
16
        $businessPageReference = null;
17
        $businessEntityMetadata = new ClassMetadata(get_class($businessPage->getBusinessEntity()));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getBusinessEntity() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...ity\VirtualBusinessPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
18
        if ($businessEntityMetadata->name !== 'Victoire\Bundle\BlogBundle\Entity\Article') {
19
            $businessPageReference = parent::buildReference($businessPage, $em);
20
        }
21
22
        return $businessPageReference;
23
    }
24
}
25