Completed
Push — master ( 3bb683...8cf661 )
by Korotkov
01:35
created

app/web/Supports/HttpErrors.php (5 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\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
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

// 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...
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):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 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-Flow

One 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 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 with throw at this point:

// 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 Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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 Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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
}