| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\ParserTests; |
| 5 |
|
| 6 |
class Item { |
| 7 |
/** @var string The type of this item. */ |
| 8 |
public $type; |
| 9 |
/** @var string The filename containing this item. */ |
| 10 |
public $filename; |
| 11 |
/** @var int The line number of the start of this item. */ |
| 12 |
public $lineNumStart; |
| 13 |
/** @var int The line number of the end of this item. */ |
| 14 |
public $lineNumEnd; |
| 15 |
/** @var ?string An optional comment describing this item. */ |
| 16 |
public $comment; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param array $props Common item properties, including type. |
| 20 |
* @param ?string $comment Optional comment describing the item |
| 21 |
*/ |
| 22 |
public function __construct( array $props, ?string $comment = null ) { |
| 23 |
$this->type = $props['type']; |
| 24 |
$this->filename = $props['filename']; |
| 25 |
$this->lineNumStart = $props['lineNumStart']; |
| 26 |
$this->lineNumEnd = $props['lineNumEnd']; |
| 27 |
$this->comment = $comment ?: null; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Return a friendly error message related to this item. |
| 32 |
* @param string $desc The error description. |
| 33 |
* @param ?string $text Optional additional context. |
| 34 |
* @return string The error message string, including the line number and |
| 35 |
* filename of this item. |
| 36 |
*/ |
| 37 |
public function errorMsg( string $desc, ?string $text = null ): string { |
| 38 |
$start = $this->lineNumStart; |
| 39 |
$end = $this->lineNumEnd; |
| 40 |
$lineDesc = $end > $start ? "lines $start-$end" : "line $start"; |
| 41 |
$fileDesc = $this->filename; // trim path in future? |
| 42 |
$extraText = $text ? ": $text" : ""; |
| 43 |
return "$desc on $lineDesc of $fileDesc$extraText"; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Throw an error related to this item. |
| 48 |
* @param string $desc The error description. |
| 49 |
* @param ?string $text Optional additional context. |
| 50 |
* @throws \Error |
| 51 |
* @return never |
| 52 |
*/ |
| 53 |
public function error( string $desc, ?string $text = null ) { |
| 54 |
throw new \Error( $this->errorMsg( $desc, $text ) ); |
| 55 |
} |
| 56 |
} |