Completed
Push — develop ( b9ad6f...544074 )
by Kirill
15:05 queued 11s
created

EloquentQueryBuilderMiddleware::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
1
<?php
2
namespace App\Gitter\Middlewares;
3
4
use App\Gitter\Response;
5
use App\Room;
6
use App\Message;
7
use App\Gitter\MiddlewareInterface;
8
use BigShark\SQLToBuilder\BuilderClass;
9
10
/**
11
 * Class EloquentQueryBuilderMiddleware
12
 * @package App\Bot\Middlewares
13
 */
14
class EloquentQueryBuilderMiddleware implements MiddlewareInterface
15
{
16
    /**
17
     * @param Message $message
18
     * @param \Closure $next
19
     * @return string|null
20
     */
21
    public function handle(Message $message, \Closure $next)
22
    {
23
        $matches = $message->text->escape()->matches('(?:select|insert).*?');
24
25
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26
            try {
27
                $response = (new BuilderClass((string)$message->text->escape()))
28
                    ->convert();
29
30
                return (new Response($response))->code('php');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \App\Gitter\...esponse))->code('php'); (App\Gitter\Response) is incompatible with the return type documented by App\Gitter\Middlewares\E...ilderMiddleware::handle of type string|null.

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...
31
            } catch (\Throwable $e) {
32
33
                return (new Response('Ошибка при анализе вашего SQL запроса'))->italic();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \App\Gitter\... (App\Gitter\Response) is incompatible with the return type documented by App\Gitter\Middlewares\E...ilderMiddleware::handle of type string|null.

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...
34
            }
35
        }
36
37
        return $next($message);
38
    }
39
}
40
41