Completed
Push — master ( 389cae...29b40e )
by Richard
06:27
created

CompilesVars::compile_var()   D

Complexity

Conditions 16
Paths 52

Size

Total Lines 96
Code Lines 52

Duplication

Lines 18
Ratio 18.75 %

Code Coverage

Tests 46
CRAP Score 18.7369

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 96
ccs 46
cts 59
cp 0.7796
rs 4.8736
nc 52
cc 16
eloc 52
nop 3
crap 18.7369

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Analysis;
12
13
use Lechimp\Dicto\Variables as Vars;
14
use Lechimp\Dicto\Variables\Variable;
15
16
/**
17
 * Implementation for Query::compile_var.
18
 */
19
trait CompilesVars {
20
    /**
21
     * Get a builder to create queries.
22
     *
23
     * @return  QueryBuilder
24
     */
25
    abstract public function builder();
26
27
    /**
28
     * Compile a variable to an SQL statement over a named table.
29
     *
30
     * @param   string          $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     * @param   Vars\Variable   $var
32
     * @param   bool            $negate
33
     * @return  string|CompositeExpression
34
     */ 
35 46
    public function compile_var($table_name, Vars\Variable $var, $negate = false) {
36 46
        $b = $this->builder()->expr();
37
        // Since SQL does not have a statement for negating while expressions,
38
        // we need to negate the single conditions in the expression, which
39
        // most often is the equality operator here.
40 46
        if (!$negate) {
41
            $eq_op = function($l, $r) use ($b) {
42 46
                return $b->eq($l, $r);
43 46
            };
44 46
        }
45
        else {
46 18
            $eq_op = function($l, $r) use ($b) {
47 18
                return $b->neq($l, $r);
48 18
            };
49
        }
50
51
        // Pattern matching on variable type.
52
53 46
        if ($var instanceof Vars\AsWellAs) {
54
            // normal case: $left_condition or $right_condition
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55 3 View Code Duplication
            if (!$negate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56 3
                return $b->orX
57 3
                    ( $this->compile_var($table_name, $var->left())
58 3
                    , $this->compile_var($table_name, $var->right())
59 3
                    );
60
            }
61
            // negated case: not ($left_condition or $right_condition)
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
            //             = not $left_condition and not $right_condition
63 View Code Duplication
            if ($negate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
                return $b->andX
65
                    ( $this->compile_var($table_name, $var->left(), true)
66
                    , $this->compile_var($table_name, $var->right(), true)
67
                    );
68
            }
69
        }
70 46 View Code Duplication
        if ($var instanceof Vars\ButNot) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71 18
            return $b->andX
72 18
                ( $this->compile_var($table_name, $var->left())
73 18
                , $this->compile_var($table_name, $var->right(), true)
74 18
                );
75
        }
76 46
        if ($var instanceof Vars\Classes) {
77 41
            return $eq_op("$table_name.type", $b->literal(Variable::CLASS_TYPE));
78
        }
79 43
        if ($var instanceof Vars\Everything) {
80 23
            return $eq_op($b->literal(1), $b->literal(1));
81
        }
82 43
        if ($var instanceof Vars\Files) {
83
            return $eq_op("$table_name.type", $b->literal(Variable::FILE_TYPE));
84
        }
85 43
        if ($var instanceof Vars\Functions) {
86 12
            return $eq_op("$table_name.type", $b->literal(Variable::FUNCTION_TYPE));
87
        }
88 40
        if ($var instanceof Vars\Globals) {
89 19
            return $eq_op("$table_name.type", $b->literal(Variable::GLOBAL_TYPE));
90
        }
91 34
        if ($var instanceof Vars\LanguageConstruct) {
92
            // normal case : language construct and name matches
93 7
            if (!$negate) {
94 7
                return $b->andX
95 7
                    ( $eq_op("$table_name.type", $b->literal(Variable::LANGUAGE_CONSTRUCT_TYPE))
96 7
                    , $eq_op("$table_name.name", $b->literal($var->construct_name()))
97 7
                    );
98
            }
99
            // negated case: not (language construct and name matches)
100
            //             = not language construct or not name matches
101
            else {
102
103
                return $b->orX
104
                    ( $eq_op("$table_name.type", $b->literal(Variable::LANGUAGE_CONSTRUCT_TYPE))
105
                    , $eq_op("$table_name.name", $b->literal($var->construct_name()))
106
                    );
107
            }
108
        }
109 29
        if ($var instanceof Vars\Methods) {
110
            return $eq_op("$table_name.type", $b->literal(Variable::METHOD_TYPE));
111
        }
112 29
        if ($var instanceof Vars\WithName) {
113
            // normal case : $condition_left AND regexp matches
114 29
            if (!$negate) {
115 11
                return $b->andX
116 11
                    ( $this->compile_var($table_name, $var->variable())
117 11
                    , "$table_name.name REGEXP ".$b->literal('^'.$var->regexp().'$')
118 11
                    );
119
            }
120
            // negated case: not ($condition_left AND regexp matches)
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
            //             = not $condition_left OR not regexp matches
122
            else {
123 18
                return $b->orX
124 18
                    ( $this->compile_var($table_name, $var->variable(), true)
125 18
                    , "$table_name.name NOT REGEXP ".$b->literal('^'.$var->regexp().'$')
126 18
                    );
127
            }
128
        }
129
        throw new \LogicException("Can't compile var-type '".get_class($var)."'");
130
    }
131
132
}
133