Method   A
last analyzed

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
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
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 CondorcetPHP\Condorcet\Algo;
12
13
use CondorcetPHP\Condorcet\{CondorcetVersion, Election, Result};
14
use CondorcetPHP\Condorcet\Throwable\CondorcetException;
15
16
// Generic for Algorithms
17
abstract class Method
18
{
19
    use CondorcetVersion;
20
21
    public static $MaxCandidates = null;
22
23
    protected $_selfElection;
24
    protected $_Result;
25
26 95
    public function __construct (Election $mother)
27
    {
28 95
        $this->_selfElection = $mother;
29
30 95
        if (!is_null(static::$MaxCandidates) && $this->_selfElection->countCandidates() > static::$MaxCandidates) :
31 2
            throw new CondorcetException(101, static::METHOD_NAME[0].' is configured to accept only '.static::$MaxCandidates.' candidates');
32
        endif;
33 93
    }
34
35 22
    public function getResult () : Result
36
    {
37
        // Cache
38 22
        if ( $this->_Result !== null ) :
39 11
            return $this->_Result;
40
        endif;
41
42 22
        $this->compute();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CondorcetPHP\Condorcet\Algo\Method as the method compute() does only exist in the following sub-classes of CondorcetPHP\Condorcet\Algo\Method: CondorcetPHP\Condorcet\A...ethods\Borda\BordaCount, CondorcetPHP\Condorcet\A...ods\Borda\DowdallSystem, CondorcetPHP\Condorcet\A...ds\Dodgson\DodgsonQuick, CondorcetPHP\Condorcet\Algo\Methods\Ftpt\Ftpt, CondorcetPHP\Condorcet\A...antRunoff\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...
43
44 22
        return $this->_Result;
45
    }
46
47
    abstract protected function getStats () : array;
48
49 87
    protected function createResult (array $result) : Result
50
    {
51 87
    	return new Result (
52 87
            static::METHOD_NAME[0],
53 87
            get_class($this),
54 87
    		$this->_selfElection,
55
    		$result,
56 87
            $this->getStats()
57
    	);
58
    }
59
}
60