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

Name::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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