Completed
Push — master ( 3a58ae...412d43 )
by Christian
08:50 queued 33s
created

FlattenException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Exception;
13
14
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
15
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
16
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
17
18
if (method_exists(LegacyFlattenException::class, 'createFromThrowable')) {
19
    /**
20
     * @internal
21
     */
22
    class FlattenException extends LegacyFlattenException
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Debug\Exception\FlattenException has been deprecated with message: since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception\FlattenException instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
    {
24
    }
25
} else {
26
    /**
27
     * @internal
28
     */
29
    class FlattenException extends LegacyFlattenException
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type FOS\RestBundle\Exception\FlattenException has been defined more than once; this definition is ignored, only the first definition in this file (L22-24) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Deprecated Code introduced by
The class Symfony\Component\Debug\Exception\FlattenException has been deprecated with message: since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception\FlattenException instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
    {
31
        private $traceAsString;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
33
        /**
34
         * @return static
35
         */
36
        public static function createFromThrowable(\Throwable $exception, int $statusCode = null, array $headers = [])
37
        {
38
            $e = new static();
39
            $e->setMessage($exception->getMessage());
40
            $e->setCode($exception->getCode());
41
42
            if ($exception instanceof HttpExceptionInterface) {
43
                $statusCode = $exception->getStatusCode();
44
                $headers = array_merge($headers, $exception->getHeaders());
45
            } elseif ($exception instanceof RequestExceptionInterface) {
46
                $statusCode = 400;
47
            }
48
49
            if (null === $statusCode) {
50
                $statusCode = 500;
51
            }
52
53
            $e->setStatusCode($statusCode);
54
            $e->setHeaders($headers);
55
            $e->setTraceFromThrowable($exception);
56
            $e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
0 ignored issues
show
Bug introduced by
The class FOS\RestBundle\Exception\FatalThrowableError 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...
57
            $e->setFile($exception->getFile());
58
            $e->setLine($exception->getLine());
59
60
            $previous = $exception->getPrevious();
61
62
            if ($previous instanceof \Throwable) {
63
                $e->setPrevious(static::createFromThrowable($previous));
0 ignored issues
show
Documentation introduced by
static::createFromThrowable($previous) is of type this<FOS\RestBundle\Exception\FlattenException>, but the function expects a object<self>.

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...
64
            }
65
66
            return $e;
67
        }
68
69
        public function setTraceFromThrowable(\Throwable $throwable)
70
        {
71
            $this->traceAsString = $throwable->getTraceAsString();
0 ignored issues
show
Bug introduced by
The property traceAsString cannot be accessed from this context as it is declared private in class Symfony\Component\Debug\Exception\FlattenException.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
72
73
            return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
74
        }
75
    }
76
}
77