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