Completed
Push — master ( e666ea...addc16 )
by Richard
05:13
created

Name   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 10
loc 42
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

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 8 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\Graph\Node;
14
use Lechimp\Dicto\Definition\ArgumentParser;
15
16
/**
17
 * Name is a property, right?
18
 */
19
class Name extends Property {
20
    /**
21
     * @inheritdocs
22
     */
23 39
    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...
24 39
        return "name";
25
    }
26
27
    /**
28
     * @inheritdocs
29
     */
30 4
    public function fetch_arguments(ArgumentParser $parser) {
31 4
        $regexp = $parser->fetch_string();
32 4
        return array($regexp);
33
    }
34
35
    /**
36
     * @inheritdocs
37
     */
38 27 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...
39 27
        if (count($arguments) != 1) {
40
            return false;
41
        }
42 27
        $regexp = $arguments[0];
43 27
        if (!is_string($regexp) || @preg_match("%^$regexp\$%", "") === false) {
44
            return false;
45
        }
46 27
        return true;
47
    }
48
49
    /**
50
     * @inheritdocs
51
     */
52 23
    public function compile(array &$arguments) {
53 23
        assert('$this->arguments_are_valid($arguments)');
54 23
        $regexp = $arguments[0];
55 23
        return function (Node $n) use ($regexp) {
56 22
            return $n->has_property("name")
57 22
                && preg_match("%^$regexp\$%", $n->property("name")) == 1;
58 23
        };
59
    }
60
}
61