Completed
Push — master ( 7bef7a...470c23 )
by Richard
07:03
created

_PropertyMatches::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 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\Graph\Predicate;
12
13
use Lechimp\Dicto\Regexp;
14
use Lechimp\Dicto\Graph\Predicate;
15
use Lechimp\Dicto\Graph\Entity;
16
17
/**
18
 * A predicate that is true if a property of the entity matches a regex.
19
 */
20
class _PropertyMatches extends Predicate {
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var string
28
     */
29
    protected $regexp;
30
31 34
    public function __construct($name, Regexp $regexp) {
32 34
        assert('is_string($name)');
33 34
        $this->name = $name;
34 34
        $this->regexp = $regexp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $regexp of type object<Lechimp\Dicto\Regexp> is incompatible with the declared type string of property $regexp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35 34
    }
36
37
    /**
38
     * @inheritdocs
39
     */
40
    public function _compile() {
41
        $name = $this->name;
42
        return function(Entity $e) use ($name) {
43
            if (!$e->has_property($name)) {
44
                return false;
45
            }
46
            return $this->regexp->match($e->property($name));
0 ignored issues
show
Bug introduced by
The method match cannot be called on $this->regexp (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
47
        };
48
    }
49
50
    /**
51
     * @inheritdocs
52
     */
53 32
    public function compile_to_source(array &$custom_closures) {
54 32
        $name = $this->name;
55
        // If we didn't do this, backslash would not be escaped enough when
56
        // eval'ing them.
57 32
        $regexp = '%^'.str_replace("\\", "\\\\", $this->regexp->raw()).'$%';
0 ignored issues
show
Bug introduced by
The method raw cannot be called on $this->regexp (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
        return
59
            "   \$value = \n".
60 32
            "       \$e->has_property(\"$name\")\n".
61 32
            "       && (preg_match(\"$regexp\", \$e->property(\"$name\")) == 1);\n";
62
    }
63
64
    /**
65
     * @inheritdocs
66
     */
67 11
    public function for_types(array $existing_types) {
68 11
        return $existing_types;
69
    }
70
}
71