Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Expectation
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 compare
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikimedia\WikiPEG;
4
5class Expectation implements \JsonSerializable {
6    /** @var string */
7    public $type;
8    /** @var string|null */
9    public $value;
10    /** @var string */
11    public $description;
12
13    /**
14     * @param array{type:string,value?:?string,description:string} $info
15     *   The expectation array, which comes from the generated code, with keys:
16     *   - type: The failed node type
17     *   - value: The actual string which failed to match, may be absent for some node types
18     *   - description: A readable description of the value
19     */
20    public function __construct( $info ) {
21        $this->type = $info['type'];
22        $this->value = $info['value'] ?? null;
23        $this->description = $info['description'];
24    }
25
26    /**
27     * Compare two Expectation objects, and return a value less than, equal to,
28     * or greater than zero, depending on whether $a is less than, equal to, or
29     * greater than $b respectively.
30     *
31     * This is used to sort expectations before combining them into SyntaxError
32     * descriptions.
33     *
34     * @param Expectation $a
35     * @param Expectation $b
36     * @return int
37     */
38    public static function compare( Expectation $a, Expectation $b ) {
39        return $a->type <=> $b->type
40                      ?: $a->value <=> $b->value
41                      ?: $a->description <=> $b->description;
42    }
43
44    /**
45     * Emit a JSON serialization similar to JS, for testing
46     * @return array
47     */
48    #[\ReturnTypeWillChange]
49    public function jsonSerialize(): array {
50        return [
51            'type' => $this->type,
52            'value' => $this->value,
53            'description' => $this->description
54        ];
55    }
56}