Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.22% covered (success)
97.22%
35 / 36
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SingleMethodLinks
97.22% covered (success)
97.22%
35 / 36
90.00% covered (success)
90.00%
9 / 10
20
0.00% covered (danger)
0.00%
0 / 1
 instanceWithParam
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 withParam
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 asMergedWith
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 withoutShape
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 withOffsetPushedToAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 asAllParamsMovedToKeys
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasParam
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParamOffsets
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withOnlyParams
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
n/a
0 / 0
n/a
0 / 0
2
1<?php declare( strict_types=1 );
2
3namespace SecurityCheckPlugin;
4
5use ast\Node;
6
7/**
8 * Links for a single method
9 */
10class SingleMethodLinks {
11    /**
12     * @var ParamLinksOffsets[]
13     */
14    private $params = [];
15
16    public static function instanceWithParam( int $i, int $initialFlags ): self {
17        static $singletons = [];
18        $singletonKey = "$i-$initialFlags";
19        if ( !isset( $singletons[$singletonKey] ) ) {
20            $singletons[$singletonKey] = ( new self() )->withParam( $i, $initialFlags );
21        }
22        return $singletons[$singletonKey];
23    }
24
25    public function withParam( int $i, int $flags ): self {
26        $ret = clone $this;
27        $ret->params[$i] = ParamLinksOffsets::getInstance( $flags );
28        return $ret;
29    }
30
31    public function asMergedWith( self $other ): self {
32        $ret = clone $this;
33        foreach ( $other->params as $i => $otherPar ) {
34            if ( isset( $ret->params[$i] ) ) {
35                $ret->params[$i] = $ret->params[$i]->asMergedWith( $otherPar );
36            } else {
37                $ret->params[$i] = $otherPar;
38            }
39        }
40        return $ret;
41    }
42
43    public function withoutShape( self $other ): self {
44        $ret = clone $this;
45        foreach ( $other->params as $i => $otherParam ) {
46            if ( isset( $ret->params[$i] ) ) {
47                $newParamData = $ret->params[$i]->withoutShape( $otherParam );
48                if ( !$newParamData->isEmpty() ) {
49                    $ret->params[$i] = $newParamData;
50                } else {
51                    unset( $ret->params[$i] );
52                }
53            }
54        }
55        return $ret;
56    }
57
58    /**
59     * @param Node|string|int|null $offset
60     */
61    public function withOffsetPushedToAll( $offset ): self {
62        $ret = clone $this;
63        foreach ( $ret->params as $i => $_ ) {
64            $ret->params[$i] = $ret->params[$i]->withOffsetPushed( $offset );
65        }
66        return $ret;
67    }
68
69    /**
70     * @return self
71     */
72    public function asAllParamsMovedToKeys(): self {
73        $ret = new self;
74        foreach ( $this->params as $i => $offsets ) {
75            $ret->params[$i] = $offsets->asMovedToKeys();
76        }
77        return $ret;
78    }
79
80    /**
81     * @todo Try to avoid this method
82     * @return ParamLinksOffsets[]
83     */
84    public function getParams(): array {
85        return $this->params;
86    }
87
88    /**
89     * @param int $x
90     * @return bool
91     */
92    public function hasParam( int $x ): bool {
93        return isset( $this->params[$x] );
94    }
95
96    /**
97     * @note This will fail hard if unset.
98     * @param int $x
99     * @return ParamLinksOffsets
100     */
101    public function getParamOffsets( int $x ): ParamLinksOffsets {
102        return $this->params[$x];
103    }
104
105    /**
106     * @param int[] $params
107     */
108    public function withOnlyParams( array $params ): self {
109        $ret = clone $this;
110        $ret->params = array_intersect_key( $this->params, array_fill_keys( $params, 1 ) );
111        return $ret;
112    }
113
114    /**
115     * @codeCoverageIgnore
116     */
117    public function __toString(): string {
118        $paramBits = [];
119        foreach ( $this->params as $k => $paramOffsets ) {
120            $paramBits[] = "$k: { " . $paramOffsets->__toString() . ' }';
121        }
122        return '[ ' . implode( ', ', $paramBits ) . ' ]';
123    }
124}