Completed
Push — master ( 526007...a3325f )
by Michael
03:04
created

PollManager::getPolls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace AppBundle\Utils;
4
5
use AppBundle\Entity\{
6
	Choice, Poll, PollType
7
};
8
use Doctrine\Common\Persistence\ManagerRegistry;
9
10
class PollManager
11
{
12
    protected $doctrineRegistry;
13
    protected $electionManager;
14
15
	/**
16
	 * Constructor.
17
	 *
18
	 * @param \Doctrine\Common\Persistence\ManagerRegistry $doctrineRegistry
19 1
	 * @param \AppBundle\Utils\ElectionManager             $electionManager
20
	 */
21 1
	public function __construct(ManagerRegistry $doctrineRegistry, ElectionManager $electionManager)
22 1
    {
23 1
        $this->doctrineRegistry = $doctrineRegistry;
24 1
        $this->electionManager = $electionManager;
25
    }
26
27
	public function getPolls($sort, boolean $current = false): array
0 ignored issues
show
Unused Code introduced by
The parameter $sort is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
	{
29
		if ($current) {
30
			return $this->getCurrentPolls();
31 1
		}
32
	}
33 1
34
    /**
35 1
     * Get an array of current polls (objects).
36
     *
37
     * @return array
38
     */
39
    public function getCurrentPolls(): array
40
    {
41
        $currentPolls = $this->doctrineRegistry->getEntityManager()->getRepository('AppBundle:Poll')->findByActive(true);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ManagerRegistry as the method getEntityManager() does only exist in the following implementations of said interface: Doctrine\Bundle\DoctrineBundle\Registry.

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...
42
43
        return $currentPolls;
44
    }
45
46
    /**
47
     * Get an array of eligible votes.
48
     *
49
     * @param AppBundle/Entity/PollType $type
0 ignored issues
show
Documentation introduced by
The doc-type AppBundle/Entity/PollType could not be parsed: Unknown type name "AppBundle/Entity/PollType" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
50
     *
51
     * @return array
52
     */
53
    public function getEligibleVoters(PollType $type): array
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55 1
        return [];
56
    }
57 1
58
    /**
59 1
     * Get an array of all polls (objects).
60
     *
61
     * @return array
62
     */
63
    public function getAllPolls(): array
64
    {
65
	    $polls = $this->doctrineRegistry->getEntityManager()->getRepository('AppBundle:Poll')->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ManagerRegistry as the method getEntityManager() does only exist in the following implementations of said interface: Doctrine\Bundle\DoctrineBundle\Registry.

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...
66
67
        return $polls;
68
    }
69
70
    /**
71
     * Get poll statistics.
72
     *
73
     * @param Poll $poll
74
     *
75
     * @return array
76
     */
77
    public function getPollStats(Poll $poll): array
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        // If Poll is election then reject
80
81
        // Get standard stats
82
        return [];
83
    }
84
85
	/**
86
	 * Get the result of the poll (in terms of a choice).
87
	 *
88
	 * @param Poll $poll
89
	 *
90
	 * @return \AppBundle\Entity\Choice
91
	 */
92
    public function getPollResult(Poll $poll): Choice
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        // If election then call Election Manager
95
96
        // Calculate winning choice
97
        return;
98
    }
99
100
    public function getStandardPollVotes(Poll $poll): array
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
        return [];
103
    }
104
105
    public function markPollClosed(Poll $poll)
0 ignored issues
show
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        return;
108
    }
109
}
110