Completed
Push — master ( bd87f9...9971b9 )
by Richard
06:03
created

Name   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 17.54 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 88.24%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 0
cbo 3
dl 10
loc 57
ccs 15
cts 17
cp 0.8824
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A fetch_arguments() 0 4 1
A arguments_are_valid() 10 10 4
A compile() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Variables;
12
13
use Lechimp\Dicto\Definition\ArgumentParser;
14
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
15
16
/**
17
 * Name is a property, right?
18
 */
19
class Name extends Property {
20
    /**
21
     * Name of the property.
22
     *
23
     * @return  string
24
     */
25 34
    public function name() {
1 ignored issue
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
26 34
        return "name";
27
    }
28
29
    /**
30
     * Fetch arguments for the Property from a stream of tokens during parsing.
31
     *
32
     * @param   ArgumentParser  $parser
33
     * @return  array
34
     */
35 7
    public function fetch_arguments(ArgumentParser $parser) {
36 7
        $regexp = $parser->fetch_string();
37 7
        return array($regexp);
38
    }
39
40
    /**
41
     * Check if the given arguments are valid for the property.
42
     *
43
     * @param   array   $arguments
44
     * @return  bool 
45
     */
46 36 View Code Duplication
    public function arguments_are_valid(array &$arguments) {
0 ignored issues
show
Duplication introduced by
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...
47 36
        if (count($arguments) != 1) {
48
            return false;
49
        }
50 36
        $regexp = $arguments[0];
51 36
        if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) {
52
            return false;
53
        }
54 36
        return true;
55
    }
56
57
    /**
58
     * Compile the property to an SQL expression.
59
     *
60
     * @param   array               $argument
0 ignored issues
show
Documentation introduced by
There is no parameter named $argument. Did you maybe mean $arguments?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
61
     * @param   ExpressionBuilder   $builder
62
     * @param   string              $table_name
63
     * @param   bool                $negate
64
     * @return  string|CompositeExpression
65
     */
66 29
    public function compile(array &$arguments, ExpressionBuilder $builder, $table_name, $negate = false) {
67 29
        assert('$this->arguments_are_valid($arguments)');
68 29
        if (!$negate) {
69 11
            return "$table_name.name REGEXP ".$builder->literal('^'.$arguments[0].'$');
70
        }
71
        else {
72 18
            return "$table_name.name NOT REGEXP ".$builder->literal('^'.$arguments[0].'$');
73
        }
74
    }
75
}
76