Passed
Push — master ( 77b992...62880d )
by Sebastian
02:14
created

Mailcode_Variables   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseString() 0 21 4
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Variables} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Variables
7
 * @see Mailcode_Variables
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Handler for all variable-related tasks.
16
 *
17
 * @package Mailcode
18
 * @subpackage Variables
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Variables
22
{
23
    const REGEX_VARIABLE_NAME = '/\$\s*([A-Z0-9]+)\s*\.\s*([A-Z0-9]+)/sx';
24
    
25
   /**
26
    * Parses the specified string to find all variable names contained within, if any.
27
    * 
28
    * @param string $subject
29
    * @return Mailcode_Variables_Collection_Regular
30
    */
31
    public function parseString(string $subject) : Mailcode_Variables_Collection_Regular
32
    {
33
        $collection = new Mailcode_Variables_Collection_Regular();
34
        
35
        $matches = array();
36
        preg_match_all(self::REGEX_VARIABLE_NAME, $subject, $matches, PREG_PATTERN_ORDER);
37
        
38
        if(!isset($matches[0]) || empty($matches[0]))
39
        {
40
            return $collection;
41
        }
42
        
43
        foreach($matches[0] as $idx => $matchedText)
44
        {
45
            $path = $matches[1][$idx];
46
            $name = $matches[2][$idx];
47
            
48
            $collection->add(new Mailcode_Variables_Variable($path, $name, $matchedText));
49
        }
50
        
51
        return $collection;
52
    }
53
}
54