Completed
Pull Request — master (#82)
by
unknown
02:08
created

GenericSqlVisitor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 74
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A visitParameter() 0 5 1
A visitAccess() 0 4 1
A visitArray() 0 6 1
A visitOperator() 0 16 3
1
<?php
2
3
namespace RulerZ\Target;
4
5
use Hoa\Ruler\Model as AST;
6
7
use RulerZ\Compiler\Context;
8
use RulerZ\Exception\OperatorNotFoundException;
9
use RulerZ\Model;
10
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions;
11
12
/**
13
 * Base class for sql-related visitors.
14
 */
15
class GenericSqlVisitor extends GenericVisitor
16
{
17
    use Polyfill\AccessPath;
18
19
    /**
20
     * @var Context
21
     */
22
    protected $context;
23
24
    /**
25
     * Allow star operator.
26
     *
27
     * @var bool
28
     */
29
    protected $allowStarOperator = true;
30
31
    /**
32
     * @param bool $allowStarOperator Whether to allow the star operator or not (ie: implicit support of unknown operators).
33
     */
34
    public function __construct(Context $context, OperatorsDefinitions $operators, $allowStarOperator = true)
35
    {
36
        parent::__construct($operators);
37
38
        $this->context = $context;
39
        $this->allowStarOperator = (bool) $allowStarOperator;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function visitParameter(Model\Parameter $element, &$handle = null, $eldnah = null)
46
    {
47
        // make it a placeholder
48
        return ':'.$element->getName();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function visitAccess(AST\Bag\Context $element, &$handle = null, $eldnah = null)
55
    {
56
        return $this->flattenAccessPath($element);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function visitArray(AST\Bag\RulerArray $element, &$handle = null, $eldnah = null)
63
    {
64
        $array = parent::visitArray($element, $handle, $eldnah);
65
66
        return sprintf('(%s)', implode(', ', $array));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return sprintf('(%s)', implode(', ', $array)); (string) is incompatible with the return type declared by the interface RulerZ\Compiler\RuleVisitor::visitArray of type array.

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...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function visitOperator(AST\Operator $element, &$handle = null, $eldnah = null)
73
    {
74
        try {
75
            return parent::visitOperator($element, $handle, $eldnah);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::visitOper...ent, $handle, $eldnah); (RulerZ\Target\Operators\...erators\RuntimeOperator) is incompatible with the return type declared by the interface RulerZ\Compiler\RuleVisitor::visitOperator of type Hoa\Consistency\Xcallable.

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...
76
        } catch (OperatorNotFoundException $e) {
77
            if (!$this->allowStarOperator) {
78
                throw $e;
79
            }
80
        }
81
82
        $arguments = array_map(function ($argument) use (&$handle, $eldnah) {
83
            return $argument->accept($this, $handle, $eldnah);
84
        }, $element->getArguments());
85
86
        return sprintf('%s(%s)', $element->getName(), implode(', ', $arguments));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return sprintf('%s(%s)',...ode(', ', $arguments)); (string) is incompatible with the return type declared by the interface RulerZ\Compiler\RuleVisitor::visitOperator of type Hoa\Consistency\Xcallable.

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...
87
    }
88
}
89