Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| IndexMapOffset | |
100.00% |
14 / 14 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| newFromText | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| newFromArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * Copyright 2023 Wikimedia Foundation |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * @file |
| 20 | * @license Apache-2.0 |
| 21 | * @license MIT |
| 22 | * @license GPL-2.0-or-later |
| 23 | * @license LGPL-2.1-or-later |
| 24 | */ |
| 25 | |
| 26 | namespace Wikimedia\Minify; |
| 27 | |
| 28 | /** |
| 29 | * A class representing a line/column offset into a combined generated file, |
| 30 | * for index map generation. |
| 31 | * |
| 32 | * Or it can represent the past-the-end offset of a single specified file, |
| 33 | * that is, the number of lines in the file and the number of columns in |
| 34 | * the last line of the file. |
| 35 | */ |
| 36 | class IndexMapOffset { |
| 37 | /** @var int */ |
| 38 | public $line; |
| 39 | /** @var int */ |
| 40 | public $column; |
| 41 | |
| 42 | /** |
| 43 | * @param int $line The zero-based line number |
| 44 | * @param int $column The zero-based column number |
| 45 | */ |
| 46 | public function __construct( int $line, int $column ) { |
| 47 | $this->line = $line; |
| 48 | $this->column = $column; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Count the number of lines and columns in the specified string, and |
| 53 | * create an IndexMapOffset representing the corresponding size. |
| 54 | * |
| 55 | * @param string $text |
| 56 | * @return self |
| 57 | */ |
| 58 | public static function newFromText( string $text ) { |
| 59 | $lines = substr_count( $text, "\n" ); |
| 60 | $lastBreakPos = strrpos( $text, "\n" ); |
| 61 | if ( $lastBreakPos === false ) { |
| 62 | $columns = Utils::getJsLength( $text ); |
| 63 | } else { |
| 64 | $columns = Utils::getJsLength( substr( $text, $lastBreakPos + 1 ) ); |
| 65 | } |
| 66 | return new self( $lines, $columns ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Restore an IndexMapOffset which was serialized with toArray(). |
| 71 | * |
| 72 | * @param array $data |
| 73 | * @return self |
| 74 | */ |
| 75 | public static function newFromArray( array $data ) { |
| 76 | return new self( $data[0], $data[1] ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert the object to plain data. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function toArray(): array { |
| 85 | return [ $this->line, $this->column ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Advance the offset, assuming a file of the specified size was added |
| 90 | * to the combined file. |
| 91 | * |
| 92 | * @param IndexMapOffset $nextSize |
| 93 | * @return void |
| 94 | */ |
| 95 | public function add( self $nextSize ) { |
| 96 | if ( $nextSize->line > 0 ) { |
| 97 | $this->line += $nextSize->line; |
| 98 | $this->column = $nextSize->column; |
| 99 | } else { |
| 100 | $this->column += $nextSize->column; |
| 101 | } |
| 102 | } |
| 103 | } |