Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.48% |
43 / 46 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
| LazyAttributes | |
93.48% |
43 / 46 |
|
75.00% |
9 / 12 |
27.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| offsetExists | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| offsetGet | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| offsetSet | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| offsetUnset | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getValues | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getObjects | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| count | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getIterator | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| merge | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| clone | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\Tokenizer; |
| 5 | |
| 6 | /** |
| 7 | * An Attributes implementation which defers interpretation of regex match |
| 8 | * results until the caller requires them. |
| 9 | * |
| 10 | * This should not be directly instantiated outside of Tokenizer. |
| 11 | */ |
| 12 | class LazyAttributes implements Attributes { |
| 13 | /** @var callable */ |
| 14 | private $interpreter; |
| 15 | |
| 16 | /** @var mixed */ |
| 17 | private $data; |
| 18 | |
| 19 | /** @var string[] */ |
| 20 | private $attributes; |
| 21 | |
| 22 | /** @var Attribute[] */ |
| 23 | private $attrObjects; |
| 24 | |
| 25 | /** |
| 26 | * @param mixed $data |
| 27 | * @param callable $interpreter |
| 28 | */ |
| 29 | public function __construct( $data, callable $interpreter ) { |
| 30 | $this->interpreter = $interpreter; |
| 31 | $this->data = $data; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Initialize the attributes array |
| 36 | */ |
| 37 | private function init() { |
| 38 | if ( $this->attributes === null ) { |
| 39 | $func = $this->interpreter; |
| 40 | $this->attributes = $func( $this->data ); |
| 41 | // @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal |
| 42 | $this->interpreter = null; |
| 43 | $this->data = null; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | public function offsetExists( $offset ): bool { |
| 49 | if ( $this->attributes === null ) { |
| 50 | $this->init(); |
| 51 | } |
| 52 | return isset( $this->attributes[$offset] ); |
| 53 | } |
| 54 | |
| 55 | /** @inheritDoc */ |
| 56 | public function &offsetGet( $offset ): string { |
| 57 | if ( $this->attributes === null ) { |
| 58 | $this->init(); |
| 59 | } |
| 60 | return $this->attributes[$offset]; |
| 61 | } |
| 62 | |
| 63 | /** @inheritDoc */ |
| 64 | public function offsetSet( $offset, $value ): void { |
| 65 | if ( $this->attributes === null ) { |
| 66 | $this->init(); |
| 67 | } |
| 68 | $this->attributes[$offset] = $value; |
| 69 | if ( $this->attrObjects !== null ) { |
| 70 | $this->attrObjects[$offset] = new Attribute( $offset, null, null, $offset, $value ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** @inheritDoc */ |
| 75 | public function offsetUnset( $offset ): void { |
| 76 | if ( $this->attributes === null ) { |
| 77 | $this->init(); |
| 78 | } |
| 79 | unset( $this->attributes[$offset] ); |
| 80 | unset( $this->attrObjects[$offset] ); |
| 81 | } |
| 82 | |
| 83 | /** @inheritDoc */ |
| 84 | public function getValues() { |
| 85 | if ( $this->attributes === null ) { |
| 86 | $this->init(); |
| 87 | } |
| 88 | return $this->attributes; |
| 89 | } |
| 90 | |
| 91 | /** @inheritDoc */ |
| 92 | public function getObjects() { |
| 93 | if ( $this->attrObjects === null ) { |
| 94 | if ( $this->attributes === null ) { |
| 95 | $this->init(); |
| 96 | } |
| 97 | $result = []; |
| 98 | foreach ( $this->attributes as $key => $value ) { |
| 99 | // Undo PHP's magic conversion of numeric string keys to ints |
| 100 | $name = (string)$key; |
| 101 | $result[$key] = new Attribute( $name, null, null, $name, $value ); |
| 102 | } |
| 103 | $this->attrObjects = $result; |
| 104 | } |
| 105 | return $this->attrObjects; |
| 106 | } |
| 107 | |
| 108 | /** @inheritDoc */ |
| 109 | public function count(): int { |
| 110 | if ( $this->attributes === null ) { |
| 111 | return count( $this->data ); |
| 112 | } |
| 113 | return count( $this->attributes ); |
| 114 | } |
| 115 | |
| 116 | public function getIterator(): \ArrayIterator { |
| 117 | if ( $this->attributes === null ) { |
| 118 | $this->init(); |
| 119 | } |
| 120 | return new \ArrayIterator( $this->attributes ); |
| 121 | } |
| 122 | |
| 123 | /** @inheritDoc */ |
| 124 | public function merge( Attributes $other ) { |
| 125 | if ( $this->attributes === null ) { |
| 126 | $this->init(); |
| 127 | } |
| 128 | foreach ( $other as $name => $value ) { |
| 129 | if ( !isset( $this->attributes[$name] ) ) { |
| 130 | $this->attributes[$name] = $value; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** @inheritDoc */ |
| 136 | public function clone() { |
| 137 | return $this; |
| 138 | } |
| 139 | } |