Completed
Branch master (fa2fc5)
by Boudry
05:50 queued 02:29
created

Method::getResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
namespace Condorcet\Algo;
12
13
use Condorcet\CondorcetException;
14
use Condorcet\CondorcetVersion;
15
use Condorcet\Election;
16
use Condorcet\Result;
17
18
// Generic for Algorithms
19
abstract class Method
20
{
21
    use CondorcetVersion;
22
23
    public static $_maxCandidates = null;
24
25
    protected $_selfElection;
26
    protected $_Result;
27
28 92
    public function __construct (Election $mother)
29
    {
30 92
        $this->_selfElection = $mother;
31
32 92
        if (!is_null(static::$_maxCandidates) && $this->_selfElection->countCandidates() > static::$_maxCandidates) :
33 2
            throw new CondorcetException(101, static::METHOD_NAME[0].' is configured to accept only '.static::$_maxCandidates.' candidates');
34
        endif;
35 90
    }
36
37 21
    public function getResult () : Result
38
    {
39
        // Cache
40 21
        if ( $this->_Result !== null ) :
41 11
            return $this->_Result;
42
        endif;
43
44 21
        $this->compute();
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Condorcet\Algo\Method as the method compute() does only exist in the following sub-classes of Condorcet\Algo\Method: Condorcet\Algo\Methods\BordaCount, Condorcet\Algo\Methods\DodgsonQuick, Condorcet\Algo\Methods\DowdallSystem, Condorcet\Algo\Methods\Ftpt, Condorcet\Algo\Methods\InstantRunoff. 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...
45
46 21
        return $this->_Result;
47
    }
48
49
    abstract protected function getStats () : array;
50
51 84
    protected function createResult (array $result) : Result
52
    {
53 84
    	return new Result (
54 84
            static::METHOD_NAME[0],
55 84
            get_class($this),
56 84
    		$this->_selfElection,
57 84
    		$result,
58 84
            $this->getStats()
59
    	);
60
    }
61
}
62