Completed
Push — master ( ffac26...9b96f1 )
by Michał
02:32
created

Str::tokenize()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 7
nop 1
1
<?php namespace nyx\console\input\formats\parsers;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\console\input\formats;
8
9
/**
10
 * String Parser
11
 *
12
 * The input string is expected to be formatted according to GNU guidelines (same as PHP's internal argv handling).
13
 * @see https://www.gnu.org/software/guile/manual/html_node/Command-Line-Format.html for more information.
14
 *
15
 * This class uses an algorithm ported from Symfony's Console, written by (c) Fabien Potencier <[email protected]>
16
 *
17
 * @version     0.1.0
18
 * @author      Fabien Potencier <[email protected]>
19
 * @author      Michal Chojnacki <[email protected]>
20
 * @copyright   2012-2017 Nyx Dev Team
21
 * @link        https://github.com/unyx/nyx
22
 */
23
class Str extends Argv implements formats\interfaces\Tokenizer
24
{
25
    /**
26
     * The token matching expressions.
27
     */
28
    const PATTERN_BARE   = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
29
    const PATTERN_QUOTED = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @param   string  $input
35
     * @throws  core\exceptions\InvalidArgumentType When the given input is not a string.
36
     * @throws  \RuntimeException                   Upon failing to parse a given substring.
37
     */
38
    public function tokenize($input) : iterable
39
    {
40
        if (!is_string($input)) {
41
            throw new core\exceptions\InvalidArgumentType($input, ['string']);
42
        }
43
44
        // Change line breaks, tabs etc. into whitespaces.
45
        $length = strlen($input);
46
        $cursor = 0;
47
        $i      = 0;
48
        $tokens = new formats\tokens\Argv;
49
50
        while ($cursor < $length) {
51
            if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
52
            } elseif (preg_match('/([^="\'\s]+?)(=?)('.static::PATTERN_QUOTED.'+)/A', $input, $match, null, $cursor)) {
53
                $tokens->set($i++, $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1))));
54
            } elseif (preg_match('/'.static::PATTERN_QUOTED.'/A', $input, $match, null, $cursor)) {
55
                $tokens->set($i++, stripcslashes(substr($match[0], 1, -1)));
56
            } elseif (preg_match('/'.static::PATTERN_BARE.'/A', $input, $match, null, $cursor)) {
57
                $tokens->set($i++, stripcslashes($match[1]));
58
            } else {
59
                throw new \RuntimeException('Failed to parse string near "... '.substr($input, $cursor, 10).' ..."');
60
            }
61
62
            $cursor += strlen($match[0]);
63
        }
64
65
        return $tokens;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $tokens; (nyx\console\input\formats\tokens\Argv) is incompatible with the return type declared by the interface nyx\console\input\format...ces\Tokenizer::tokenize of type nyx\console\input\formats\interfaces\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
    }
67
}
68