Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FauxHookHandlerArray
77.78% covered (warning)
77.78%
14 / 18
42.86% covered (danger)
42.86%
3 / 7
12.33
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 offsetExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 offsetGet
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 offsetSet
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 offsetUnset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHandler
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getIterator
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\HookContainer;
4
5use InvalidArgumentException;
6use LogicException;
7use OutOfBoundsException;
8
9/**
10 * @internal
11 */
12class FauxHookHandlerArray implements \ArrayAccess, \IteratorAggregate {
13
14    private HookContainer $hookContainer;
15
16    private string $name;
17
18    private ?array $handlers = null;
19
20    /**
21     * @param HookContainer $hookContainer
22     * @param string $name
23     */
24    public function __construct( HookContainer $hookContainer, string $name ) {
25        $this->hookContainer = $hookContainer;
26        $this->name = $name;
27    }
28
29    /**
30     * @inheritDoc
31     */
32    #[\ReturnTypeWillChange]
33    public function offsetExists( $offset ) {
34        return $this->getHandler( $offset ) !== null;
35    }
36
37    /**
38     * @inheritDoc
39     */
40    #[\ReturnTypeWillChange]
41    public function offsetGet( $offset ) {
42        $handler = $this->getHandler( $offset );
43
44        if ( !$handler ) {
45            throw new OutOfBoundsException( "No such index in the handler list: $offset" );
46        }
47
48        return $handler;
49    }
50
51    /**
52     * @inheritDoc
53     */
54    #[\ReturnTypeWillChange]
55    public function offsetSet( $offset, $value ) {
56        if ( $offset !== null ) {
57            throw new InvalidArgumentException( '$offset must be null, this array is append only' );
58        }
59
60        $this->hookContainer->register( $this->name, $value );
61        $this->handlers = null;
62    }
63
64    /**
65     * @inheritDoc
66     * @return never
67     */
68    #[\ReturnTypeWillChange]
69    public function offsetUnset( $offset ) {
70        throw new LogicException( 'unset is not supported for hook handler arrays' );
71    }
72
73    private function getHandler( $offset ) {
74        if ( $this->handlers === null ) {
75            // NOTE: getHandlerCallbacks() only exists to support this.
76            //       It should be deleted when we no longer need it here.
77            $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
78        }
79
80        return $this->handlers[$offset] ?? null;
81    }
82
83    #[\ReturnTypeWillChange]
84    public function getIterator() {
85        if ( $this->handlers === null ) {
86            // NOTE: getHandlerCallbacks() only exists to support this.
87            //       It should be deleted when we no longer need it here.
88            $this->handlers = $this->hookContainer->getHandlerCallbacks( $this->name );
89        }
90
91        return new \ArrayIterator( $this->handlers );
92    }
93}