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

Regexp::match_beginning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
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;
12
13
/**
14
 * Small wrapper around preg.
15
 */
16
class Regexp {
17
    /**
18
     * @var string
19
     */
20
    protected $regexp;
21
22
    /**
23
     * @var string
24
     */
25
    protected $delim = "%";
26
27 127
    public function __construct($regexp) {
28 127
        assert('is_string($regexp)');
29 127
        if (@preg_match($this->delim.$regexp.$this->delim, "") === false) {
30 2
            throw new \InvalidArgumentException("Invalid regexp '$regexp'");
31
        }
32 125
        $this->regexp = $regexp;
33 125
    }
34
35
    /**
36
     * @return  string
37
     */
38 100
    public function raw() {
39 100
        return $this->regexp;
40
    }
41
42
    /**
43
     * Match a string with the regexp.
44
     *
45
     * @param   string      $str
46
     * @param   bool        $dotall
47
     * @param   array|null  $matches
48
     * @return  bool
49
     */
50 12
    public function match($str, $dotall = false, &$matches = null) {
51 12
        if (!$dotall) {
52 12
            return preg_match($this->delim."^".$this->regexp.'$'.$this->delim, $str, $matches) === 1;
53
        }
54
        else {
55 1
            return preg_match($this->delim."^".$this->regexp.'$'.$this->delim."s", $str, $matches) === 1;
56
        }
57
    }
58
59
    /**
60
     * Match the beginning of a string with the regexp.
61
     *
62
     * @param   string      $str
63
     * @param   bool        $dotall
64
     * @param   array|null  $matches
65
     * @return  bool
66
     */
67 66 View Code Duplication
    public function match_beginning($str, $dotall = false, &$matches = null) {
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...
68 66
        if (!$dotall) {
69 5
            return preg_match($this->delim."^".$this->regexp.$this->delim, $str, $matches) === 1;
70
        }
71
        else {
72 62
            return preg_match($this->delim."^".$this->regexp.$this->delim."s", $str, $matches) === 1;
73
        }
74
    }
75
76
    /**
77
     * Search a string with the regexp.
78
     *
79
     * @param   string      $str
80
     * @param   bool        $dotall
81
     * @param   array|null  $matches
82
     * @return  bool
83
     */
84 14 View Code Duplication
    public function search($str, $dotall = false, &$matches = null) {
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...
85 14
        if (!$dotall) {
86 14
            return preg_match($this->delim.$this->regexp.$this->delim, $str, $matches) === 1;
87
        }
88
        else {
89 4
            return preg_match($this->delim.$this->regexp.$this->delim."s", $str, $matches) === 1;
90
        }
91
    }
92
}
93
94