Passed
Push — master ( bba3ec...e9806b )
by Luke
02:25
created

RetryablePackageFailureException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 23
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getExceptionType() 0 3 1
A getExceptionCodeName() 0 3 1
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\Exception;
12
13
use Throwable;
14
15
/**
16
 * Retryable failure occurred during obtaining a package.
17
 */
18
class RetryablePackageFailureException extends AbstractException {
19
    /**
20
     * @override \Exception
21
     */
22
    public function __construct(Throwable $previous) {
23
        parent::__construct(
24
                $previous->getMessage(), $previous->getCode(), $previous);
25
    }
26
27
    /**
28
     * @override \ComponentManager\Exception\AbstractException
29
     */
30
    public function getExceptionType() {
31
        return $this->getPrevious()->getExceptionType();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method getExceptionType() does only exist in the following sub-classes of Exception: ComponentManager\Exception\AbstractException, ComponentManager\Excepti...mponentProjectException, ComponentManager\Excepti...llationFailureException, ComponentManager\Exception\InvalidProjectException, ComponentManager\Exception\MoodleApiException, ComponentManager\Exception\MoodleException, ComponentManager\Exception\NotImplementedException, ComponentManager\Exception\PackageFailureException, ComponentManager\Exception\PlatformException, ComponentManager\Excepti...PackageFailureException, ComponentManager\Excepti...tisfiedVersionException, ComponentManager\Exception\VersionControlException. 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...
32
    }
33
34
    /**
35
     * @override \ComponentManager\Exception\AbstractException
36
     */
37
    public function getExceptionCodeName() {
38
        return $this->getPrevious()->getExceptionCodeName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method getExceptionCodeName() does only exist in the following sub-classes of Exception: ComponentManager\Exception\AbstractException, ComponentManager\Excepti...mponentProjectException, ComponentManager\Excepti...llationFailureException, ComponentManager\Exception\InvalidProjectException, ComponentManager\Exception\MoodleApiException, ComponentManager\Exception\MoodleException, ComponentManager\Exception\NotImplementedException, ComponentManager\Exception\PackageFailureException, ComponentManager\Exception\PlatformException, ComponentManager\Excepti...PackageFailureException, ComponentManager\Excepti...tisfiedVersionException, ComponentManager\Exception\VersionControlException. 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...
39
    }
40
}
41