Issues (78)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/IntervalFinder.php (56 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
2
/**
3
 * Finds intervals inside strings
4
 *
5
 * PHP version 7+
6
 *
7
 * @category   IntervalParser
8
 * @author     Ekin H. Bayar <[email protected]>
9
 * @version    0.2.0
10
 */
11
namespace IntervalParser;
12
13
class IntervalFinder
14
{
15
    # Leading separator
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
16
    const LEADING_SEPARATOR = "(?<leadingSeparator>\s?(?:in)\s?)";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal (?<leadingSeparator>\s?(?:in)\s?) 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...
17
18
    # Regex to match a valid interval and any trailing string, holds the interval in $matches['interval'], the rest in $matches['trailing']
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
19
    const INTERVAL_WITH_TRAILING_DATA = "^(?<interval>(?&timepart)++)(?<trailing>.+)*?$/uix";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ^(?<interval>(?&timepart...)(?<trailing>.+)*?$/uix 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...
20
21
    # Regex to handle an input that may have multiple intervals along with leading and/or trailing data
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
22
    const MULTIPLE_INTERVALS = <<<'REGEX'
23
    ^(?<leading>.*?)?
24
     (?<sep>(?&leadingSeparator))?
25
     (?<interval>(?&timepart)++)
26
     (?<trailing>.*)
27
    /uix
28
REGEX;
29
30
    /**
31
     * @var ParserSettings
32
     */
33
    private $settings;
34
35
    /**
36
     * @var Normalizer
37
     */
38
    private $normalizer;
39
40
    /**
41
     * IntervalFinder constructor.
42
     *
43
     * Default settings are :
44
     *
45
     *  string $symbolSeparator  = ',',
46
     *  string $wordSeparator = null
47
     *
48
     * @param \IntervalParser\ParserSettings $settings
49
     * @param \IntervalParser\Normalizer $normalizer
50
     */
51 4
    public function __construct(ParserSettings $settings, Normalizer $normalizer)
52
    {
53 4
        $this->settings   = $settings;
54 4
        $this->normalizer = $normalizer;
55
    }
56
57
    /**
58
     * Looks for a valid interval along with leading and/or trailing data IF the respective flags are set.
59
     * TimeInterval is essentially DateInterval with extra information such as interval offset & length, leading/trailing data.
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
     *
61
     * @param string $input
62
     * @param int $flags
63
     * @return TimeInterval|array
64
     * @throws InvalidFlagException
65
     * @throws FormatException
66
     */
67 4
    public function find(string $input, int $flags = IntervalFlags::INTERVAL_ONLY)
68
    {
69
        if ($flags
70 4
            & ~IntervalFlags::INTERVAL_ONLY
71 4
            & ~IntervalFlags::REQUIRE_TRAILING
72 4
            & ~IntervalFlags::REQUIRE_LEADING
73 4
            & ~IntervalFlags::MULTIPLE_INTERVALS
74
        ) {  throw new InvalidFlagException("You have tried to use an invalid flag combination."); }
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal You have tried to use an invalid flag combination. 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
76 4
        if ($flags & IntervalFlags::INTERVAL_ONLY) {
77
78
            $input = $this->normalizer->normalize($input);
79
80
            $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')';
81
            $expression = $definition . Pattern::INTERVAL_ONLY;
82
83
            if (preg_match($expression, $input)) {
84
                $intervalOffset = 0;
85
                $intervalLength = strlen($input);
86
87
                # create and return the interval object
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
88
                $interval = \DateInterval::createFromDateString($input);
89
                return new TimeInterval($interval, $intervalOffset, $intervalLength);
90
            }
91
92
            throw new FormatException("Given input is not a valid interval.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input is not a valid interval. 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...
93
        }
94
95 4
        if ($flags == (IntervalFlags::REQUIRE_LEADING | IntervalFlags::REQUIRE_TRAILING)) {
96
97 1
            $expression = $this->settings->getLeadingSeparatorExpression();
98
99 1
            $leadingSeparation = preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE);
100 1
            if (!$leadingSeparation) {
101
                throw new FormatException("Allowing leading data requires using a separator. Ie. foo in <interval>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Allowing leading data re.... Ie. foo in <interval> 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...
102
            }
103
104 1
            $leadingData = $matches[1][0] ?? null;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 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...
105 1
            $intervalAndTrailingData = $matches[2][0] ?? null;
106
107
            # throw early for missing parts
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
108 1
            if (!$leadingData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $leadingData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109
                throw new FormatException("Given input does not contain a valid leading data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input does not contain a valid leading data. 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...
110
            }
111 1
            if (!$intervalAndTrailingData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $intervalAndTrailingData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
                throw new FormatException("Given input does not contain a valid interval and/or trailing data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input does not con...l and/or trailing data. 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...
113
            }
114
115 1
            $intervalOffset = $matches[2][1] ?? null;
116
117
            # If interval contains non-strtotime-compatible abbreviations, replace 'em
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
118 1
            $intervalAndTrailingData = $this->normalizer->normalize($intervalAndTrailingData);
119
120 1
            $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')';
121 1
            $expression = $definition . self::INTERVAL_WITH_TRAILING_DATA;
122
123 1
            if (preg_match($expression, $intervalAndTrailingData, $parts)) {
124
125 1
                $interval = $parts['interval'];
0 ignored issues
show
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...
126 1
                $trailingData   = $parts['trailing'];
127 1
                $intervalLength = strlen($interval);
128
129
                # create and return the interval object
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
130 1
                $interval = \DateInterval::createFromDateString($interval);
131 1
                return new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData, $trailingData);
132
            }
133
134
            throw new FormatException("Given input does not contain a valid interval and/or trailing data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input does not con...l and/or trailing data. 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...
135
        }
136
137 3
        if ($flags & IntervalFlags::REQUIRE_LEADING) {
138
139 1
            $expression = $this->settings->getLeadingSeparatorExpression();
140
141 1
            $leadingSeparation = preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE);
142 1
            if (!$leadingSeparation) {
143
                throw new FormatException("Allowing leading data requires using a separator. Ie. foo in <interval>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Allowing leading data re.... Ie. foo in <interval> 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...
144
            }
145
146 1
            $leadingData = $matches[1][0] ?? null;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 21 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...
147 1
            $intervalAndPossibleTrailingData = $matches[2][0] ?? null;
148
149 1
            if (!$leadingData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $leadingData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
150
                throw new FormatException("Could not find any valid leading data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find any valid leading data. 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...
151
            }
152
153 1
            if (!$intervalAndPossibleTrailingData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $intervalAndPossibleTrailingData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
154
                throw new FormatException("Could not find any valid interval and/or leading data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find any valid...al and/or leading data. 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...
155
            }
156
157 1
            $intervalOffset = $matches[2][1] ?? null;
158
159
            # If interval contains non-strtotime-compatible abbreviations, replace 'em
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
160 1
            $safeInterval = $this->normalizer->normalize($intervalAndPossibleTrailingData);
161
162
            # since above normalization is expected to not return any trailing data, only check for a valid interval
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
163 1
            $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')';
164 1
            $expression = $definition . Pattern::INTERVAL_ONLY;
165
166 1
            if (preg_match($expression, $safeInterval, $parts)) {
167 1
                $interval = $parts['interval'];
0 ignored issues
show
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...
168 1
                $intervalLength = strlen($interval);
169
170
                # create the interval object
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
171 1
                $interval = \DateInterval::createFromDateString($interval);
172 1
                return new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData);
173
            }
174
175
            throw new FormatException("Given input does not contain a valid interval. Keep in mind trailing data is not allowed with current flag.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input does not con...owed with current flag. 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...
This line exceeds maximum limit of 120 characters; contains 149 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
176
        }
177
178 2
        if ($flags & IntervalFlags::REQUIRE_TRAILING) {
179
180 2
            $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')';
181 2
            $expression = $definition . self::INTERVAL_WITH_TRAILING_DATA;
182
183
            # If interval contains non-strtotime-compatible abbreviations, replace 'em
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
184 2
            $safeInterval = $this->normalizer->normalize($input);
185
186
            # Separate interval from trailing data
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
187 2
            if (preg_match($expression, $safeInterval, $parts)) {
188 2
                $trailingData = $parts['trailing'] ?? null;
189 2
                $interval = $parts['interval'] ?? null;
0 ignored issues
show
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...
190
191 2
                if (!$interval) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $interval of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
192
                    throw new FormatException("Could not find any valid interval.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find any valid interval. 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...
193
                }
194
195 2
                if (!$trailingData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $trailingData of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
196
                    throw new FormatException("Could not find any valid trailing data.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Could not find any valid trailing data. 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...
197
                }
198
199 2
                $intervalLength = strlen($interval);
200 2
                $intervalOffset = 0; # since we don't allow leading data here
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
201
202
                # create the interval object
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
203 2
                $interval = \DateInterval::createFromDateString($interval);
204 2
                return new TimeInterval($interval, $intervalOffset, $intervalLength, null, $trailingData);
205
            }
206
207
            throw new FormatException("Given input does not contain a valid interval. Keep in mind leading data is not allowed with current flag.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given input does not con...owed with current flag. 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...
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
208
        }
209
210
        if ($flags & IntervalFlags::MULTIPLE_INTERVALS) {
211
212
            $payload = [];
0 ignored issues
show
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...
213
            $separator = ($this->settings->getSeparationType() == 'symbol')
214
                ? $this->settings->getSymbolSeparator()
215
                : $this->settings->getWordSeparator();
216
217
            $expression = "/(?J)\b(?:(?<match>.*?)\s?{$separator})\s?|\b(?<match>.*)/ui";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $separator instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
218
219
            if (preg_match_all($expression, $input, $intervals, PREG_SET_ORDER)) {
220
221
                $intervalSet = array_filter(array_map(function($set) {
222
                    foreach ($iter = new IntervalIterator($set) as $key => $interval) {
223
                        if ($iter->key() === 'match') {
224
                            return $interval;
225
                        }
226
                    }
227
                }, $intervals));
228
229
                foreach ($intervalSet as $key => $interval) {
230
231
                    $definition = Pattern::DEFINE . self::LEADING_SEPARATOR . Pattern::INTEGER . Pattern::TIME_PART . ')';
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
232
                    $expression = $definition . self::MULTIPLE_INTERVALS;
233
234
                    preg_match($expression, $interval, $matches);
235
                    $matches = array_filter($matches);
236
237
                    $leadingData = $matches['leading'] ?? null;
238
                    $leadingSep  = $matches['sep'] ?? null;
239
                    $interval    = $matches['interval'] ?? null;
240
                    $trailing    = $matches['trailing'] ?? null;
241
242
                    if (!$leadingData) $leadingData = $leadingSep ?? "";
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...
243
244
                    $intervalOffset = (!$leadingSep) ? 0 : strlen($leadingData) + strlen($leadingSep);
245
246
                    # If interval contains non-strtotime-compatible abbreviations, replace them
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
247
                    $safeInterval = $this->normalizer->normalize($interval . $trailing);
248
249
                    # Separate intervals from trailing data
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
250
                    if (preg_match($expression, $safeInterval, $parts)) {
251
                        $trailingData = $parts['trailing'] ?? null;
252
                        $interval = $parts['interval'] ?? null;
0 ignored issues
show
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...
253
                        if (!$interval) continue;
0 ignored issues
show
Bug Best Practice introduced by
The expression $interval of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
254
255
                        $intervalLength = strlen($interval);
256
                        # create the interval object
0 ignored issues
show
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
257
                        $interval = \DateInterval::createFromDateString($interval);
0 ignored issues
show
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...
258
                        $payload[] = new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData, $trailingData);
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
259
                    }
260
                }
261
262
                if ($payload) return $payload;
0 ignored issues
show
Bug Best Practice introduced by
The expression $payload of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
263
            }
264
        }
265
    }
266
}
267