Completed
Push — master ( 7a5f39...3a2708 )
by ARCANEDEV
09:08
created

ErrorTracker::track()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\ErrorTracker as ErrorTrackerContract;
4
use Arcanedev\LaravelTracker\Models\Error;
5
use Exception;
6
7
/**
8
 * Class     ErrorTracker
9
 *
10
 * @package  Arcanedev\LaravelTracker\Trackers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class ErrorTracker extends AbstractTracker implements ErrorTrackerContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Track the exception error.
21
     *
22
     * @param  \Exception  $exception
23
     *
24
     * @return int
25
     */
26 10
    public function track(Exception $exception)
27
    {
28
        $data = [
29 10
            'code'    => $this->getCode($exception),
30 10
            'message' => $exception->getMessage(),
31 5
        ];
32
33 10
        return Error::firstOrCreate($data)->id;
34
    }
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Other Functions
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Get the code from the exception.
42
     *
43
     * @param  \Exception  $exception
44
     *
45
     * @return int|mixed|null
46
     */
47 10
    public function getCode(Exception $exception)
48
    {
49 10
        if (method_exists($exception, 'getCode') && $code = $exception->getCode())
50 8
            return $code;
51
52 4
        if (method_exists($exception, 'getStatusCode') && $code = $exception->getStatusCode())
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 getStatusCode() does only exist in the following sub-classes of Exception: MaxMind\Exception\AuthenticationException, MaxMind\Exception\HttpException, MaxMind\Exception\InsufficientFundsException, MaxMind\Exception\InvalidRequestException, MaxMind\Exception\IpAddressNotFoundException, MaxMind\Exception\PermissionRequiredException. 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...
53 4
            return $code;
54
55
        return null;
56
    }
57
}
58