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

Method   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getResult() 0 11 2
getStats() 0 1 ?
A createResult() 0 10 1
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