Conditions | 10 |
Paths | 8 |
Total Lines | 22 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function render($request, Exception $exception) |
||
48 | { |
||
49 | if ($request->expectsJson() || $request->isJson() || $request->is(...config('pterodactyl.json_routes'))) { |
||
50 | $exception = $this->prepareException($exception); |
||
51 | |||
52 | if (config('app.debug')) { |
||
53 | $report = [ |
||
54 | 'code' => (! $this->isHttpException($exception)) ?: $exception->getStatusCode(), |
||
55 | 'message' => class_basename($exception) . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine(), |
||
56 | ]; |
||
57 | } |
||
58 | |||
59 | $response = response()->json([ |
||
60 | 'error' => (config('app.debug')) ? $exception->getMessage() : 'An unhandled exception was encountered with this request.', |
||
61 | 'exception' => ! isset($report) ?: $report, |
||
62 | ], ($this->isHttpException($exception)) ? $exception->getStatusCode() : 500, [], JSON_UNESCAPED_SLASHES); |
||
63 | |||
64 | parent::report($exception); |
||
65 | } |
||
66 | |||
67 | return (isset($response)) ? $response : parent::render($request, $exception); |
||
68 | } |
||
69 | |||
86 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: