ParserSettings::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
crap 1
1
<?php declare(strict_types = 1);
2
/** Parser Settings
3
 *
4
 * IntervalParser takes a ParserSettings object which is handy for when you want to deal with multiple intervals.
5
 *
6
 * When parsing multiple intervals if you don't supply your own settings,
7
 * the parser will try to find comma separated intervals within given input by default.
8
 * However there is no default value for $wordSeparator (actually, there is "word").
9
 *
10
 * Example:
11
 *
12
 *     $parserSettings = new ParserSettings(1, ',', 'then');
13
 *     # works for "2 days then 5 months then 2 hours"
14
 *     $intervalParser = new IntervalParser($parserSettings);
15
 *
16
 */
17
namespace IntervalParser;
18
19
class ParserSettings
20
{
21
    const SYMBOL = 0b00000000;
22
    const STRING = 0b00000001;
23
24
    private $multipleSeparationType;
25
    private $multipleSeparationSymbol;
26
    private $multipleSeparationWord;
27
    private $leadingSeparationString;
28
    private $keepLeadingSeparator;
29
30
    # Leading separator with capturing groups
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
31
    public static $leadingGroupSeparator = /* @lang text */ '/(.*)\s+(?:in)\s+(.*)/ui';
32
    public static $symbolSeparator = /* @lang text */ '/(?<first>[^,]*)\s?,\s?(?<next>.*)$/ui';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33
    public static $wordSeparator = /* @lang text */ '/^(?<first>.*?)\s?foo\s?(?<next>.*)$/ui';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
34
35
36 22
    public function __construct(
37
        string $leadingSeparationString = 'in',
38
        bool $keepLeadingSeparator = false,
39
        int $multipleSeparationType = self::SYMBOL,
40
        string $multipleSeparationSymbol = ',',
41
        string $multipleSeparationWord = 'foo'
42
    )
43
    {
44 22
        $this->leadingSeparationString = $leadingSeparationString;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
45 22
        $this->keepLeadingSeparator = $keepLeadingSeparator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46 22
        $this->multipleSeparationType = $multipleSeparationType;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47 22
        $this->multipleSeparationSymbol = $multipleSeparationSymbol;
48 22
        $this->multipleSeparationWord = $multipleSeparationWord;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49
    }
50
51 6
    public function getLeadingSeparator() : string
52
    {
53 6
        return $this->leadingSeparationString;
54
    }
55
56 4
    public function getSymbolSeparator() : string
57
    {
58 4
        return $this->multipleSeparationSymbol;
59
    }
60
61 4
    public function getWordSeparator() : string
62
    {
63 4
        return $this->multipleSeparationWord;
64
    }
65
66 4
    public function getLeadingSeparatorExpression() : string
67
    {
68 4
        $expression = preg_replace("/in/", $this->getLeadingSeparator(), self::$leadingGroupSeparator);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /in/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
69 4
        return $expression;
70
    }
71
72 2
    public function getSymbolSeparatorExpression() : string
73
    {
74 2
        $expression = preg_replace("/,/", $this->getSymbolSeparator(), self::$symbolSeparator);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /,/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
75 2
        return $expression;
76
    }
77
78 2
    public function getWordSeparatorExpression() : string
79
    {
80 2
        $expression = preg_replace("/foo/", $this->getWordSeparator(), self::$wordSeparator);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /foo/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
81 2
        return $expression;
82
    }
83
84 3
    public function keepLeadingSeparator() : bool
85
    {
86 3
        return $this->keepLeadingSeparator;
87
    }
88
89 3
    public function getSeparationType(): string
90
    {
91 3
        return ($this->multipleSeparationType == self::STRING) ? 'string' : 'symbol';
92
    }
93
}
94