Issues (3884)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

app/Exceptions/Handler.php (26 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace App\Exceptions;
4
5
// controller
6
use Bugsnag;
7
//use Illuminate\Validation\ValidationException;
8
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler;
9
use Exception;
10
use Illuminate\Auth\Access\AuthorizationException;
11
// use Symfony\Component\HttpKernel\Exception\HttpException;
12
// use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
13
use Illuminate\Database\Eloquent\ModelNotFoundException;
14
use Illuminate\Foundation\Validation\ValidationException;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
17
class Handler extends ExceptionHandler
18
{
19
    /**
20
     * A list of the exception types that should not be reported.
21
     *
22
     * @var array
23
     */
24
    protected $dontReport = [
25
//        'Symfony\Component\HttpKernel\Exception\HttpException',
26
        \Illuminate\Http\Exception\HttpResponseException::class,
27
        ValidationException::class,
28
        AuthorizationException::class,
29
        HttpResponseException ::class,
30
        ModelNotFoundException::class,
31
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
32
    ];
33
34
    /**
35
     * Report or log an exception.
36
     *
37
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
38
     *
39
     * @param \Exception $e
40
     *
41
     * @return void
42
     */
43
    public function report(Exception $e)
44
    {
45
        $debug = \Config::get('app.bugsnag_reporting');
46
        $debug = ($debug) ? 'true' : 'false';
47
        if ($debug == 'false') {
48
            Bugsnag::setBeforeNotifyFunction(function ($error) {
0 ignored issues
show
The parameter $error is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
                return false;
50
            });
51
        } else {
52
            $version = \Config::get('app.version');
53
            Bugsnag::setAppVersion($version);
54
        }
55
56
        return parent::report($e);
57
    }
58
59
    /**
60
     * Render an exception into an HTTP response.
61
     *
62
     * @param type      $request
63
     * @param Exception $e
64
     *
65
     * @return type mixed
66
     */
67
    public function render($request, Exception $e)
68
    {
69
        switch ($e) {
70
            case $e instanceof \Illuminate\Http\Exception\HttpResponseException:
71
                return parent::render($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::render($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::render of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
72 View Code Duplication
            case $e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException:
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
                return response()->json(['message' => $e->getMessage(), 'code' => $e->getStatusCode()]);
0 ignored issues
show
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: Aws\Acm\Exception\AcmException, Aws\ApiGateway\Exception\ApiGatewayException, Aws\ApplicationAutoScali...ionAutoScalingException, Aws\ApplicationDiscovery...scoveryServiceException, Aws\AutoScaling\Exception\AutoScalingException, Aws\CloudFormation\Excep...CloudFormationException, Aws\CloudFront\Exception\CloudFrontException, Aws\CloudHsm\Exception\CloudHsmException, Aws\CloudSearchDomain\Ex...udSearchDomainException, Aws\CloudSearch\Exception\CloudSearchException, Aws\CloudTrail\Exception\CloudTrailException, Aws\CloudWatchEvents\Exc...oudWatchEventsException, Aws\CloudWatchLogs\Excep...CloudWatchLogsException, Aws\CloudWatch\Exception\CloudWatchException, Aws\CodeCommit\Exception\CodeCommitException, Aws\CodeDeploy\Exception\CodeDeployException, Aws\CodePipeline\Exception\CodePipelineException, Aws\CognitoIdentityProvi...entityProviderException, Aws\CognitoIdentity\Exce...ognitoIdentityException, Aws\CognitoSync\Exception\CognitoSyncException, Aws\ConfigService\Exception\ConfigServiceException, Aws\DataPipeline\Exception\DataPipelineException, Aws\DatabaseMigrationSer...grationServiceException, Aws\DeviceFarm\Exception\DeviceFarmException, Aws\DirectConnect\Exception\DirectConnectException, Aws\DirectoryService\Exc...rectoryServiceException, Aws\DynamoDbStreams\Exce...ynamoDbStreamsException, Aws\DynamoDb\Exception\DynamoDbException, Aws\Ec2\Exception\Ec2Exception, Aws\Ecr\Exception\EcrException, Aws\Ecs\Exception\EcsException, Aws\Efs\Exception\EfsException, Aws\ElastiCache\Exception\ElastiCacheException, Aws\ElasticBeanstalk\Exc...asticBeanstalkException, Aws\ElasticLoadBalancing...oadBalancingV2Exception, Aws\ElasticLoadBalancing...cLoadBalancingException, Aws\ElasticTranscoder\Ex...sticTranscoderException, Aws\ElasticsearchService...csearchServiceException, Aws\Emr\Exception\EmrException, Aws\Exception\AwsException, Aws\Firehose\Exception\FirehoseException, Aws\GameLift\Exception\GameLiftException, Aws\Glacier\Exception\GlacierException, Aws\Iam\Exception\IamException, Aws\ImportExport\Exception\ImportExportException, Aws\Inspector\Exception\InspectorException, Aws\IotDataPlane\Exception\IotDataPlaneException, Aws\Iot\Exception\IotException, Aws\KinesisAnalytics\Exc...nesisAnalyticsException, Aws\Kinesis\Exception\KinesisException, Aws\Kms\Exception\KmsException, Aws\Lambda\Exception\LambdaException, Aws\MachineLearning\Exce...achineLearningException, Aws\MarketplaceCommerceA...merceAnalyticsException, Aws\MarketplaceMetering\...tplaceMeteringException, Aws\OpsWorks\Exception\OpsWorksException, Aws\Rds\Exception\RdsException, Aws\Redshift\Exception\RedshiftException, Aws\Route53Domains\Excep...Route53DomainsException, Aws\Route53\Exception\Route53Exception, Aws\S3\Exception\PermanentRedirectException, Aws\S3\Exception\S3Exception, Aws\ServiceCatalog\Excep...ServiceCatalogException, Aws\Ses\Exception\SesException, Aws\SnowBall\Exception\SnowBallException, Aws\Sns\Exception\SnsException, Aws\Sqs\Exception\SqsException, Aws\Ssm\Exception\SsmException, Aws\StorageGateway\Excep...StorageGatewayException, Aws\Sts\Exception\StsException, Aws\Support\Exception\SupportException, Aws\Swf\Exception\SwfException, Aws\Waf\Exception\WafException, Aws\WorkSpaces\Exception\WorkSpacesException, 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, Tymon\JWTAuth\Exceptions\InvalidClaimException, Tymon\JWTAuth\Exceptions\JWTException, Tymon\JWTAuth\Exceptions\PayloadException, Tymon\JWTAuth\Exceptions\TokenBlacklistedException, Tymon\JWTAuth\Exceptions\TokenExpiredException, Tymon\JWTAuth\Exceptions\TokenInvalidException. 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...
Bug Best Practice introduced by
The return type of return response()->json(... $e->getStatusCode())); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74 View Code Duplication
            case $e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException:
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                return response()->json(['message' => $e->getMessage(), 'code' => $e->getStatusCode()]);
0 ignored issues
show
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: Aws\Acm\Exception\AcmException, Aws\ApiGateway\Exception\ApiGatewayException, Aws\ApplicationAutoScali...ionAutoScalingException, Aws\ApplicationDiscovery...scoveryServiceException, Aws\AutoScaling\Exception\AutoScalingException, Aws\CloudFormation\Excep...CloudFormationException, Aws\CloudFront\Exception\CloudFrontException, Aws\CloudHsm\Exception\CloudHsmException, Aws\CloudSearchDomain\Ex...udSearchDomainException, Aws\CloudSearch\Exception\CloudSearchException, Aws\CloudTrail\Exception\CloudTrailException, Aws\CloudWatchEvents\Exc...oudWatchEventsException, Aws\CloudWatchLogs\Excep...CloudWatchLogsException, Aws\CloudWatch\Exception\CloudWatchException, Aws\CodeCommit\Exception\CodeCommitException, Aws\CodeDeploy\Exception\CodeDeployException, Aws\CodePipeline\Exception\CodePipelineException, Aws\CognitoIdentityProvi...entityProviderException, Aws\CognitoIdentity\Exce...ognitoIdentityException, Aws\CognitoSync\Exception\CognitoSyncException, Aws\ConfigService\Exception\ConfigServiceException, Aws\DataPipeline\Exception\DataPipelineException, Aws\DatabaseMigrationSer...grationServiceException, Aws\DeviceFarm\Exception\DeviceFarmException, Aws\DirectConnect\Exception\DirectConnectException, Aws\DirectoryService\Exc...rectoryServiceException, Aws\DynamoDbStreams\Exce...ynamoDbStreamsException, Aws\DynamoDb\Exception\DynamoDbException, Aws\Ec2\Exception\Ec2Exception, Aws\Ecr\Exception\EcrException, Aws\Ecs\Exception\EcsException, Aws\Efs\Exception\EfsException, Aws\ElastiCache\Exception\ElastiCacheException, Aws\ElasticBeanstalk\Exc...asticBeanstalkException, Aws\ElasticLoadBalancing...oadBalancingV2Exception, Aws\ElasticLoadBalancing...cLoadBalancingException, Aws\ElasticTranscoder\Ex...sticTranscoderException, Aws\ElasticsearchService...csearchServiceException, Aws\Emr\Exception\EmrException, Aws\Exception\AwsException, Aws\Firehose\Exception\FirehoseException, Aws\GameLift\Exception\GameLiftException, Aws\Glacier\Exception\GlacierException, Aws\Iam\Exception\IamException, Aws\ImportExport\Exception\ImportExportException, Aws\Inspector\Exception\InspectorException, Aws\IotDataPlane\Exception\IotDataPlaneException, Aws\Iot\Exception\IotException, Aws\KinesisAnalytics\Exc...nesisAnalyticsException, Aws\Kinesis\Exception\KinesisException, Aws\Kms\Exception\KmsException, Aws\Lambda\Exception\LambdaException, Aws\MachineLearning\Exce...achineLearningException, Aws\MarketplaceCommerceA...merceAnalyticsException, Aws\MarketplaceMetering\...tplaceMeteringException, Aws\OpsWorks\Exception\OpsWorksException, Aws\Rds\Exception\RdsException, Aws\Redshift\Exception\RedshiftException, Aws\Route53Domains\Excep...Route53DomainsException, Aws\Route53\Exception\Route53Exception, Aws\S3\Exception\PermanentRedirectException, Aws\S3\Exception\S3Exception, Aws\ServiceCatalog\Excep...ServiceCatalogException, Aws\Ses\Exception\SesException, Aws\SnowBall\Exception\SnowBallException, Aws\Sns\Exception\SnsException, Aws\Sqs\Exception\SqsException, Aws\Ssm\Exception\SsmException, Aws\StorageGateway\Excep...StorageGatewayException, Aws\Sts\Exception\StsException, Aws\Support\Exception\SupportException, Aws\Swf\Exception\SwfException, Aws\Waf\Exception\WafException, Aws\WorkSpaces\Exception\WorkSpacesException, 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, Tymon\JWTAuth\Exceptions\InvalidClaimException, Tymon\JWTAuth\Exceptions\JWTException, Tymon\JWTAuth\Exceptions\PayloadException, Tymon\JWTAuth\Exceptions\TokenBlacklistedException, Tymon\JWTAuth\Exceptions\TokenExpiredException, Tymon\JWTAuth\Exceptions\TokenInvalidException. 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...
Bug Best Practice introduced by
The return type of return response()->json(... $e->getStatusCode())); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Exceptions\Handler::render of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
76
            default:
77
                return $this->common($request, $e);
0 ignored issues
show
$e is of type object<Exception>, but the function expects a object<App\Exceptions\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return $this->common($request, $e); (App\Exceptions\type) is incompatible with the return type declared by the interface Illuminate\Contracts\Deb...xceptionHandler::render of type Symfony\Component\HttpFoundation\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
78
        }
79
    }
80
81
    /**
82
     * Function to render 500 error page.
83
     *
84
     * @param type $request
85
     * @param type $e
86
     *
87
     * @return type mixed
88
     */
89
    public function render500($request, $e)
90
    {
91
        if (config('app.debug') == true) {
92
            return parent::render($request, $e);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (render() instead of render500()). Are you sure this is correct? If so, you might want to change this to $this->render().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
93
        } elseif ($e instanceof ValidationException) {
94
            return parent::render($request, $e);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (render() instead of render500()). Are you sure this is correct? If so, you might want to change this to $this->render().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
95
        }
96
97
        return  response()->view('errors.500');
98
        //return redirect()->route('error500', []);
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
    }
100
101
    /**
102
     * Function to render 404 error page.
103
     *
104
     * @param type $request
105
     * @param type $e
106
     *
107
     * @return type mixed
108
     */
109
    public function render404($request, $e)
110
    {
111
        $seg = $request->segments();
112
        if (in_array('api', $seg)) {
113
            return response()->json(['status' => '404']);
114
        }
115
        if (config('app.debug') == true) {
116
            if ($e->getStatusCode() == '404') {
117
                return redirect()->route('error404', []);
118
            }
119
120
            return parent::render($request, $e);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (render() instead of render404()). Are you sure this is correct? If so, you might want to change this to $this->render().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
121
        }
122
123
        return redirect()->route('error404', []);
124
    }
125
126
    /**
127
     * Function to render database connection failed.
128
     *
129
     * @param type $request
130
     * @param type $e
131
     *
132
     * @return type mixed
133
     */
134
    public function renderDB($request, $e)
135
    {
136
        $seg = $request->segments();
137
        if (in_array('api', $seg)) {
138
            return response()->json(['status' => '404']);
139
        }
140
        if (config('app.debug') == true) {
141
            return parent::render($request, $e);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (render() instead of renderDB()). Are you sure this is correct? If so, you might want to change this to $this->render().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
142
        }
143
144
        return redirect()->route('error404', []);
145
    }
146
147
    /**
148
     * Common finction to render both types of codes.
149
     *
150
     * @param type $request
151
     * @param type $e
152
     *
153
     * @return type mixed
154
     */
155
    public function common($request, $e)
156
    {
157
        switch ($e) {
158
            case $e instanceof HttpException:
0 ignored issues
show
The class App\Exceptions\HttpException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
159
                return $this->render404($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->render404($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::common of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
160
            case $e instanceof NotFoundHttpException:
161
                return $this->render404($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->render404($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::common of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
162
            case $e instanceof PDOException:
0 ignored issues
show
The class App\Exceptions\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
163
                if (strpos('1045', $e->getMessage()) == true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos('1045', $e->getMessage()) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
164
                    return $this->renderDB($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderDB($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::common of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
165
                } else {
166
                    return $this->render500($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->render500($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::common of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
167
                }
168
//            case $e instanceof ErrorException:
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
169
//                if($e->getMessage() == 'Breadcrumb not found with name "" ') {
170
//                    return $this->render404($request, $e);
171
//                } else {
172
//                    return parent::render($request, $e);
173
//                }
174
            default:
175
                return $this->render500($request, $e);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->render500($request, $e); (Symfony\Component\HttpFoundation\Response) is incompatible with the return type documented by App\Exceptions\Handler::common of type App\Exceptions\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
176
        }
177
178
        return parent::render($request, $e);
0 ignored issues
show
return parent::render($request, $e); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (render() instead of common()). Are you sure this is correct? If so, you might want to change this to $this->render().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
179
    }
180
}
181