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\Web\Supports; |
|||||||||||
4 | ||||||||||||
5 | use Rudra\RouterException; |
|||||||||||
6 | ||||||||||||
7 | trait HttpErrors |
|||||||||||
8 | { |
|||||||||||
9 | ||||||||||||
10 | public function handle404($data, string $type = 'db', array $page = []) |
|||||||||||
11 | { |
|||||||||||
12 | if ($type == 'db') { |
|||||||||||
13 | if (count($data) < 1 or !$data) { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
14 | throw new RouterException('404'); |
|||||||||||
15 | } |
|||||||||||
16 | } elseif ($type == 'pagination') { |
|||||||||||
17 | if ($page['id'] > count($data)) { |
|||||||||||
18 | throw new RouterException('404'); |
|||||||||||
19 | } |
|||||||||||
20 | } |
|||||||||||
21 | } |
|||||||||||
22 | ||||||||||||
23 | public function handleApi404($data, string $type = 'db', array $page = []) |
|||||||||||
24 | { |
|||||||||||
25 | if ($type == 'db') { |
|||||||||||
26 | if (count($data) < 1 or !$data) { |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code.
Loading history...
|
||||||||||||
27 | $this->jsonResponse(['status' => 'Not Found']); |
|||||||||||
0 ignored issues
–
show
It seems like
jsonResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
||||||||||||
28 | } |
|||||||||||
29 | } elseif ($type == 'pagination') { |
|||||||||||
30 | if ($page['id'] > count($data)) { |
|||||||||||
31 | $this->jsonResponse(['status' => 'Not Found']); |
|||||||||||
0 ignored issues
–
show
It seems like
jsonResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
||||||||||||
32 | } |
|||||||||||
33 | } |
|||||||||||
34 | } |
|||||||||||
35 | ||||||||||||
36 | View Code Duplication | public function error404() |
||||||||||
0 ignored issues
–
show
This method seems to be duplicated in 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...
|
||||||||||||
37 | { |
|||||||||||
38 | $this->redirect()->responseCode('404'); |
|||||||||||
39 | ||||||||||||
40 | return $this->twig('errors/404.html.twig', [ |
|||||||||||
41 | 'title' => '404 Page Not Found :: ' . $this->data('title'), |
|||||||||||
42 | ]); |
|||||||||||
43 | } |
|||||||||||
44 | ||||||||||||
45 | View Code Duplication | public function error503() |
||||||||||
46 | { |
|||||||||||
47 | $this->redirect()->responseCode('503'); |
|||||||||||
48 | ||||||||||||
49 | return $this->twig('errors/503.html.twig', [ |
|||||||||||
50 | 'title' => '503 Service Unavailable :: ' . $this->data('title'), |
|||||||||||
51 | ]); |
|||||||||||
52 | } |
|||||||||||
53 | ||||||||||||
54 | View Code Duplication | public function error500() |
||||||||||
55 | { |
|||||||||||
56 | $this->redirect()->responseCode('503'); |
|||||||||||
57 | ||||||||||||
58 | return $this->twig('errors/503.html.twig', [ |
|||||||||||
59 | 'title' => '503 Service Unavailable :: ' . $this->data('title'), |
|||||||||||
60 | ]); |
|||||||||||
61 | } |
|||||||||||
62 | ||||||||||||
63 | /** |
|||||||||||
64 | * @param null $target |
|||||||||||
65 | * |
|||||||||||
66 | * @return mixed |
|||||||||||
67 | */ |
|||||||||||
68 | public abstract function redirect($target = null); |
|||||||||||
69 | ||||||||||||
70 | /** |
|||||||||||
71 | * @param string $template |
|||||||||||
72 | * @param array $params |
|||||||||||
73 | */ |
|||||||||||
74 | public abstract function twig(string $template, array $params = []): void; |
|||||||||||
75 | ||||||||||||
76 | /** |
|||||||||||
77 | * @param string $key |
|||||||||||
78 | * |
|||||||||||
79 | * @return string|array |
|||||||||||
80 | */ |
|||||||||||
81 | public abstract function data(string $key = null); |
|||||||||||
82 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.