Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

VotingBundle/Helper/AbstractVotingHelper.php (3 issues)

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\VotingBundle\Helper;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
/**
8
 * Abstract Helper class for Voting
9
 */
10
abstract class AbstractVotingHelper
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    protected $em;
16
17
    /**
18
     * @var string
19
     */
20
    protected $repository;
21
22
    /**
23
     * @param EntityManagerInterface $em
24
     */
25
    public function __construct(EntityManagerInterface $em)
26
    {
27
        $this->em = $em;
28
    }
29
30
    /**
31
     * @param string $reference Reference to filter the votes by
32
     *
33
     * @return array Returns an array of votes
34
     */
35
    public function byReference($reference)
36
    {
37
        return $this->em->getRepository($this->repository)
0 ignored issues
show
The method findByReference() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
            ->findByReference($reference);
39
    }
40
41
    /**
42
     * @param string $reference The reference to filter the votes by
43
     *
44
     * @return mixed Returns the count
45
     */
46
    public function countByReference($reference)
47
    {
48
        return $this->em->getRepository($this->repository)
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method countByReference() does only exist in the following implementations of said interface: Kunstmaan\VotingBundle\R...\AbstractVoteRepository, Kunstmaan\VotingBundle\R...\FacebookLikeRepository, Kunstmaan\VotingBundle\R...\FacebookSendRepository, Kunstmaan\VotingBundle\R...LinkedInShareRepository, Kunstmaan\VotingBundle\R...Down\DownVoteRepository, Kunstmaan\VotingBundle\R...UpDown\UpVoteRepository.

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...
49
            ->countByReference($reference);
50
    }
51
52
    /**
53
     * @param string $reference The reference to filter the votes by
54
     *
55
     * @return mixed Returns the sum of the values
56
     */
57
    public function getValueByReference($reference)
58
    {
59
        return $this->em->getRepository($this->repository)
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getValueByReference() does only exist in the following implementations of said interface: Kunstmaan\VotingBundle\R...\AbstractVoteRepository, Kunstmaan\VotingBundle\R...\FacebookLikeRepository, Kunstmaan\VotingBundle\R...\FacebookSendRepository, Kunstmaan\VotingBundle\R...LinkedInShareRepository, Kunstmaan\VotingBundle\R...Down\DownVoteRepository, Kunstmaan\VotingBundle\R...UpDown\UpVoteRepository.

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...
60
            ->getValueByReference($reference);
61
    }
62
}
63