ErrorTracker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 12
cts 13
cp 0.9231

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A track() 0 7 1
B getCode() 0 10 5
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\ErrorTracker as ErrorTrackerContract;
4
use Arcanedev\LaravelTracker\Support\BindingManager;
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
     |  Getters and Setters
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Get the model.
22
     *
23
     * @return \Arcanedev\LaravelTracker\Models\Error
24
     */
25 6
    protected function getModel()
26
    {
27 6
        return $this->makeModel(BindingManager::MODEL_ERROR);
28
    }
29
30
    /* -----------------------------------------------------------------
31
     |  Main Methods
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Track the exception error.
37
     *
38
     * @param  \Exception  $exception
39
     *
40
     * @return int
41
     */
42 6
    public function track(Exception $exception)
43
    {
44 6
        return $this->getModel()->newQuery()->firstOrCreate([
45 6
            'code'    => $this->getCode($exception),
46 6
            'message' => $exception->getMessage(),
47 6
        ])->getKey();
48
    }
49
50
    /* -----------------------------------------------------------------
51
     |  Other Methods
52
     | -----------------------------------------------------------------
53
     */
54
55
    /**
56
     * Get the code from the exception.
57
     *
58
     * @param  \Exception  $exception
59
     *
60
     * @return int|mixed|null
61
     */
62 6
    public function getCode(Exception $exception)
63
    {
64 6
        if (method_exists($exception, 'getCode') && $code = $exception->getCode())
65 3
            return $code;
66
67 3
        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: Illuminate\Foundation\Ht...aintenanceModeException, Illuminate\Http\Exceptions\PostTooLargeException, Symfony\Component\HttpKe...cessDeniedHttpException, Symfony\Component\HttpKe...BadRequestHttpException, Symfony\Component\HttpKe...n\ConflictHttpException, Symfony\Component\HttpKe...ption\GoneHttpException, Symfony\Component\HttpKe...Exception\HttpException, Symfony\Component\HttpKe...thRequiredHttpException, Symfony\Component\HttpKe...NotAllowedHttpException, Symfony\Component\HttpKe...AcceptableHttpException, Symfony\Component\HttpKe...n\NotFoundHttpException, Symfony\Component\HttpKe...tionFailedHttpException, Symfony\Component\HttpKe...onRequiredHttpException, Symfony\Component\HttpKe...navailableHttpException, Symfony\Component\HttpKe...nyRequestsHttpException, Symfony\Component\HttpKe...authorizedHttpException, Symfony\Component\HttpKe...ableEntityHttpException, Symfony\Component\HttpKe...dMediaTypeHttpException. 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...
68 3
            return $code;
69
70
        return null;
71
    }
72
}
73