@@ -14,11 +14,11 @@ discard block |
||
14 | 14 | /** |
15 | 15 | * StringScanner constructor. |
16 | 16 | */ |
17 | - public function __construct( string $string ) { |
|
17 | + public function __construct(string $string) { |
|
18 | 18 | $this->string = $string; |
19 | 19 | } |
20 | 20 | |
21 | - public function peek( $size = 1 ) : CharData { |
|
21 | + public function peek($size = 1) : CharData { |
|
22 | 22 | $str = substr($this->string, $this->pos, $size); |
23 | 23 | |
24 | 24 | return new CharData($str, strlen($str) < $size); |
@@ -38,19 +38,19 @@ discard block |
||
38 | 38 | |
39 | 39 | public function rewind() : void { |
40 | 40 | $this->pos--; |
41 | - if( $this->pos < 0 ) { |
|
41 | + if ($this->pos < 0) { |
|
42 | 42 | //@todo custom exception |
43 | 43 | throw new \RuntimeException('cannot rewind'); |
44 | 44 | } |
45 | 45 | } |
46 | 46 | |
47 | - public function hasPrefix( string $prefix ) : bool { |
|
47 | + public function hasPrefix(string $prefix) : bool { |
|
48 | 48 | $peek = $this->peek(strlen($prefix)); |
49 | 49 | |
50 | 50 | return $peek->getString() === $prefix; |
51 | 51 | } |
52 | 52 | |
53 | - public function substr( $start, $length ) : string { |
|
53 | + public function substr($start, $length) : string { |
|
54 | 54 | return substr($this->string, $start, $length); |
55 | 55 | } |
56 | 56 | } |
@@ -23,7 +23,7 @@ |
||
23 | 23 | /** |
24 | 24 | * LexItem constructor. |
25 | 25 | */ |
26 | - public function __construct( string $lexItemType, string $val, int $pos ) { |
|
26 | + public function __construct(string $lexItemType, string $val, int $pos) { |
|
27 | 27 | $this->lexItemType = $lexItemType; |
28 | 28 | $this->val = $val; |
29 | 29 | $this->pos = $pos; |
@@ -13,7 +13,7 @@ |
||
13 | 13 | */ |
14 | 14 | private $eof; |
15 | 15 | |
16 | - public function __construct( string $string, bool $eof ) { |
|
16 | + public function __construct(string $string, bool $eof) { |
|
17 | 17 | $this->string = $string; |
18 | 18 | $this->eof = $eof; |
19 | 19 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | public const ARG_TYPE_STRING = 'string'; |
43 | 43 | |
44 | 44 | /** @var int[] string s */ |
45 | - public const STRING_TYPES = [ self::T_STRING ]; |
|
45 | + public const STRING_TYPES = [self::T_STRING]; |
|
46 | 46 | |
47 | 47 | /** @var int[] integer d, u, c, o, x, X, b */ |
48 | 48 | public const INTEGER_TYPES = [ |
@@ -112,15 +112,15 @@ discard block |
||
112 | 112 | * PrintfLexeme::ARG_TYPE_STRING |
113 | 113 | */ |
114 | 114 | public function argType() : string { |
115 | - if( in_array($this->lexItemType, self::INTEGER_TYPES, true) ) { |
|
115 | + if (in_array($this->lexItemType, self::INTEGER_TYPES, true)) { |
|
116 | 116 | return self::ARG_TYPE_INT; |
117 | 117 | } |
118 | 118 | |
119 | - if( in_array($this->lexItemType, self::DOUBLE_TYPES, true) ) { |
|
119 | + if (in_array($this->lexItemType, self::DOUBLE_TYPES, true)) { |
|
120 | 120 | return self::ARG_TYPE_DOUBLE; |
121 | 121 | } |
122 | 122 | |
123 | - if( in_array($this->lexItemType, self::STRING_TYPES, true) ) { |
|
123 | + if (in_array($this->lexItemType, self::STRING_TYPES, true)) { |
|
124 | 124 | return self::ARG_TYPE_STRING; |
125 | 125 | } |
126 | 126 |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require __DIR__ . '/../vendor/autoload.php'; |
|
3 | +require __DIR__.'/../vendor/autoload.php'; |
|
4 | 4 | |
5 | 5 | $emitter = new \donatj\Printf\LexemeEmitter(); |
6 | 6 | $parser = new \donatj\Printf\Parser($emitter); |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | |
10 | 10 | $lexemes = $emitter->getLexemes(); |
11 | 11 | |
12 | -foreach( $lexemes as $lexeme ) { |
|
13 | - echo $lexeme->getLexItemType() . ' -> '; |
|
12 | +foreach ($lexemes as $lexeme) { |
|
13 | + echo $lexeme->getLexItemType().' -> '; |
|
14 | 14 | echo var_export($lexeme->getVal(), true); |
15 | 15 | |
16 | - if( $lexeme instanceof \donatj\Printf\ArgumentLexeme ) { |
|
17 | - echo ' arg type: ' . $lexeme->argType(); |
|
16 | + if ($lexeme instanceof \donatj\Printf\ArgumentLexeme) { |
|
17 | + echo ' arg type: '.$lexeme->argType(); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | echo PHP_EOL; |
@@ -8,5 +8,5 @@ |
||
8 | 8 | * @param \donatj\Printf\Lexeme $lexItem |
9 | 9 | * @internal This is for use by the Parser |
10 | 10 | */ |
11 | - public function emit( Lexeme $lexItem ) : void; |
|
11 | + public function emit(Lexeme $lexItem) : void; |
|
12 | 12 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @internal |
19 | 19 | */ |
20 | - public function __construct( Lexeme ...$lexItems ) { |
|
20 | + public function __construct(Lexeme ...$lexItems) { |
|
21 | 21 | $this->lexItems = $lexItems; |
22 | 22 | } |
23 | 23 | |
@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | * This is useful for checking if a printf string parsed without error. |
28 | 28 | */ |
29 | 29 | public function getInvalid() : ?Lexeme { |
30 | - foreach( $this->lexItems as $lexItem ) { |
|
31 | - if( $lexItem->getLexItemType() === Lexeme::T_INVALID ) { |
|
30 | + foreach ($this->lexItems as $lexItem) { |
|
31 | + if ($lexItem->getLexItemType() === Lexeme::T_INVALID) { |
|
32 | 32 | return $lexItem; |
33 | 33 | } |
34 | 34 | } |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | /** |
40 | 40 | * @internal |
41 | 41 | */ |
42 | - public function offsetSet( $offset, $value ) { |
|
43 | - if( $offset === null ) { |
|
42 | + public function offsetSet($offset, $value) { |
|
43 | + if ($offset === null) { |
|
44 | 44 | $this->lexItems[] = $value; |
45 | 45 | } else { |
46 | 46 | $this->lexItems[$offset] = $value; |
@@ -50,21 +50,21 @@ discard block |
||
50 | 50 | /** |
51 | 51 | * @internal |
52 | 52 | */ |
53 | - public function offsetExists( $offset ) { |
|
53 | + public function offsetExists($offset) { |
|
54 | 54 | return isset($this->lexItems[$offset]); |
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
58 | 58 | * @internal |
59 | 59 | */ |
60 | - public function offsetUnset( $offset ) { |
|
60 | + public function offsetUnset($offset) { |
|
61 | 61 | unset($this->lexItems[$offset]); |
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
65 | 65 | * @internal |
66 | 66 | */ |
67 | - public function offsetGet( $offset ) { |
|
67 | + public function offsetGet($offset) { |
|
68 | 68 | return isset($this->lexItems[$offset]) ? $this->lexItems[$offset] : null; |
69 | 69 | } |
70 | 70 | |
@@ -99,11 +99,11 @@ discard block |
||
99 | 99 | public function argTypes() : array { |
100 | 100 | $noNumInc = 1; |
101 | 101 | $args = []; |
102 | - foreach( $this->lexItems as $item ) { |
|
103 | - if( $item instanceof ArgumentLexeme ) { |
|
102 | + foreach ($this->lexItems as $item) { |
|
103 | + if ($item instanceof ArgumentLexeme) { |
|
104 | 104 | $type = $item->argType(); |
105 | 105 | |
106 | - if( $item->getArg() !== null ) { |
|
106 | + if ($item->getArg() !== null) { |
|
107 | 107 | $args[$item->getArg()] = $type; |
108 | 108 | } else { |
109 | 109 | $args[$noNumInc] = $type; |
@@ -11,7 +11,7 @@ |
||
11 | 11 | * @param \donatj\Printf\Lexeme $lexItem |
12 | 12 | * @internal This is for use by the Parser |
13 | 13 | */ |
14 | - public function emit( Lexeme $lexItem ) : void { |
|
14 | + public function emit(Lexeme $lexItem) : void { |
|
15 | 15 | $this->lexemes[] = $lexItem; |
16 | 16 | } |
17 | 17 |
@@ -19,19 +19,19 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @param \donatj\Printf\Emitter $emitter The given Emitter to emit Lexemes as parsed |
21 | 21 | */ |
22 | - public function __construct( Emitter $emitter ) { |
|
22 | + public function __construct(Emitter $emitter) { |
|
23 | 23 | $this->emitter = $emitter; |
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Parses a printf string and emit parsed lexemes to the configured Emitter |
28 | 28 | */ |
29 | - public function parseStr( string $string ) : void { |
|
29 | + public function parseStr(string $string) : void { |
|
30 | 30 | $lexer = new StringLexer($string); |
31 | 31 | |
32 | - while( ($next = $lexer->next()) && !$next->isEof() ) { |
|
33 | - if( $next->getString() === '%' ) { |
|
34 | - if( $lexer->hasPrefix('%') ) { |
|
32 | + while (($next = $lexer->next()) && !$next->isEof()) { |
|
33 | + if ($next->getString() === '%') { |
|
34 | + if ($lexer->hasPrefix('%')) { |
|
35 | 35 | $this->emitter->emit( |
36 | 36 | new Lexeme(Lexeme::T_LITERAL_STRING, '%', $lexer->pos()) |
37 | 37 | ); |
@@ -50,11 +50,11 @@ discard block |
||
50 | 50 | } |
51 | 51 | } |
52 | 52 | |
53 | - private function lexString( Emitter $emitter, StringLexer $lexer ) : void { |
|
53 | + private function lexString(Emitter $emitter, StringLexer $lexer) : void { |
|
54 | 54 | $pos = $lexer->pos(); |
55 | 55 | $buffer = ''; |
56 | - while( ($next = $lexer->next()) && !$next->isEof() ) { |
|
57 | - if( $next->getString() === '%' ) { |
|
56 | + while (($next = $lexer->next()) && !$next->isEof()) { |
|
57 | + if ($next->getString() === '%') { |
|
58 | 58 | $lexer->rewind(); |
59 | 59 | break; |
60 | 60 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | ); |
68 | 68 | } |
69 | 69 | |
70 | - private function lexSprintf( Emitter $emitter, StringLexer $lexer ) : void { |
|
70 | + private function lexSprintf(Emitter $emitter, StringLexer $lexer) : void { |
|
71 | 71 | $pos = $lexer->pos(); |
72 | 72 | $next = $lexer->next(); |
73 | 73 | |
@@ -78,17 +78,17 @@ discard block |
||
78 | 78 | $leftJustified = null; |
79 | 79 | $precision = null; |
80 | 80 | |
81 | - if( $next->getString() !== '0' && ctype_digit($next->getString()) ) { |
|
81 | + if ($next->getString() !== '0' && ctype_digit($next->getString())) { |
|
82 | 82 | $lexer->rewind(); |
83 | 83 | $int = $this->eatInt($lexer); |
84 | - if( $lexer->hasPrefix('$') ) { |
|
84 | + if ($lexer->hasPrefix('$')) { |
|
85 | 85 | $lexer->next(); |
86 | 86 | $next = $lexer->next(); |
87 | 87 | $arg = $int; |
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
91 | - switch( $next->getString() ) { |
|
91 | + switch ($next->getString()) { |
|
92 | 92 | case '0': |
93 | 93 | $padChar = '0'; |
94 | 94 | $next = $lexer->next(); |
@@ -103,23 +103,23 @@ discard block |
||
103 | 103 | $next = $lexer->next(); |
104 | 104 | } |
105 | 105 | |
106 | - if( $next->getString() === '-' ) { |
|
106 | + if ($next->getString() === '-') { |
|
107 | 107 | $leftJustified = true; |
108 | 108 | $next = $lexer->next(); |
109 | 109 | } |
110 | 110 | |
111 | - if( $padChar !== null ) { |
|
111 | + if ($padChar !== null) { |
|
112 | 112 | $lexer->rewind(); |
113 | 113 | $peek = $lexer->peek(); |
114 | - if( ctype_digit($peek->getString()) ) { |
|
114 | + if (ctype_digit($peek->getString())) { |
|
115 | 115 | $padWidth = $this->eatInt($lexer); |
116 | 116 | } |
117 | 117 | |
118 | 118 | $next = $lexer->next(); |
119 | 119 | } |
120 | 120 | |
121 | - if( $next->getString() === '.' ) { |
|
122 | - if( ctype_digit($lexer->peek()->getString()) ) { |
|
121 | + if ($next->getString() === '.') { |
|
122 | + if (ctype_digit($lexer->peek()->getString())) { |
|
123 | 123 | $precision = $this->eatInt($lexer); |
124 | 124 | } |
125 | 125 | |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | $tType = Lexeme::T_INVALID; |
130 | - if( in_array($next->getString(), ArgumentLexeme::VALID_T_TYPES, true) ) { |
|
130 | + if (in_array($next->getString(), ArgumentLexeme::VALID_T_TYPES, true)) { |
|
131 | 131 | $tType = $next->getString(); |
132 | 132 | } |
133 | 133 | |
@@ -148,10 +148,10 @@ discard block |
||
148 | 148 | ); |
149 | 149 | } |
150 | 150 | |
151 | - private function eatInt( StringLexer $lexer ) : string { |
|
151 | + private function eatInt(StringLexer $lexer) : string { |
|
152 | 152 | $int = ''; |
153 | - while( ($next = $lexer->next()) && !$next->isEof() ) { |
|
154 | - if( !ctype_digit($next->getString()) ) { |
|
153 | + while (($next = $lexer->next()) && !$next->isEof()) { |
|
154 | + if (!ctype_digit($next->getString())) { |
|
155 | 155 | $lexer->rewind(); |
156 | 156 | break; |
157 | 157 | } |