GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a67098...e6e3d2 )
by SignpostMarv
01:42
created

AbstractPropertyNotThingableError   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
1
<?php
2
/**
3
* Exceptions.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use Throwable;
12
use TypeError;
13
14
/**
15
* Error thrown when a property is not thingable.
16
*/
17
abstract class AbstractPropertyNotThingableError extends TypeError
18
{
19
    /**
20
    * Wraps to TypeError::__construct().
21
    *
22
    * @param string $thing the type of thing that the $property cannot do, i.e. writeable, nullable
23
    * @param string $className name of the class on which the property is not thingable
24
    * @param string $property name of the property which is not thingable
25
    * @param int $code @see TypeError::__construct()
26
    * @param Throwable|null $previous @see TypeError::__construct()
27
    */
28
    public function __construct(
29
        string $thing,
30
        string $className,
31
        string $property,
32
        int $code = 0,
33
        Throwable $previous = null
34
    ) {
35
        parent::__construct(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TypeError as the method __construct() does only exist in the following sub-classes of TypeError: SignpostMarv\DaftObject\...opertyNotThingableError, SignpostMarv\DaftObject\...rtyNotNullableException, SignpostMarv\DaftObject\...tyNotWriteableException. 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...
36
            sprintf(
37
                'Property not %s: %s::$%s',
38
                $thing,
39
                $className,
40
                $property
41
            ),
42
            $code,
43
            $previous
44
        );
45
    }
46
}
47