Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
24 / 28
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
SingleMethodLinks
85.71% covered (warning)
85.71%
24 / 28
91.67% covered (success)
91.67%
11 / 12
20.05
0.00% covered (danger)
0.00%
0 / 1
 newWithParam
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addParam
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mergeWith
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 pushOffsetToAll
100.00% covered (success)
100.00%
2 / 2
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
 keepOnlyParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllPreservedFlags
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __clone
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 __toString
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
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    /**
17     * @param int $i
18     * @return self
19     */
20    public static function newWithParam( int $i ): self {
21        $ret = new self;
22        $ret->addParam( $i );
23        return $ret;
24    }
25
26    /**
27     * @param int $i
28     */
29    public function addParam( int $i ): void {
30        $this->params[$i] = ParamLinksOffsets::newAll();
31    }
32
33    /**
34     * @param self $other
35     */
36    public function mergeWith( self $other ): void {
37        foreach ( $other->params as $i => $otherPar ) {
38            if ( isset( $this->params[$i] ) ) {
39                $this->params[$i]->mergeWith( $otherPar );
40            } else {
41                $this->params[$i] = $otherPar;
42            }
43        }
44    }
45
46    /**
47     * @param Node|string|int|null $offset
48     */
49    public function pushOffsetToAll( $offset ): void {
50        foreach ( $this->params as $i => $_ ) {
51            $this->params[$i]->pushOffset( $offset );
52        }
53    }
54
55    /**
56     * @return self
57     */
58    public function asAllParamsMovedToKeys(): self {
59        $ret = new self;
60        foreach ( $this->params as $i => $offsets ) {
61            $ret->params[$i] = $offsets->asMovedToKeys();
62        }
63        return $ret;
64    }
65
66    /**
67     * @todo Try to avoid this method
68     * @return ParamLinksOffsets[]
69     */
70    public function getParams(): array {
71        return $this->params;
72    }
73
74    /**
75     * @param int $x
76     * @return bool
77     */
78    public function hasParam( int $x ): bool {
79        return isset( $this->params[$x] );
80    }
81
82    /**
83     * @note This will fail hard if unset.
84     * @param int $x
85     * @return ParamLinksOffsets
86     */
87    public function getParamOffsets( int $x ): ParamLinksOffsets {
88        return $this->params[$x];
89    }
90
91    /**
92     * @param int[] $params
93     */
94    public function keepOnlyParams( array $params ): void {
95        $this->params = array_intersect_key( $this->params, array_fill_keys( $params, 1 ) );
96    }
97
98    /**
99     * @note This should only be used by MethodLinks::getAllPreservedFlags
100     * @return int
101     */
102    public function getAllPreservedFlags(): int {
103        $ret = SecurityCheckPlugin::NO_TAINT;
104        foreach ( $this->params as $offsets ) {
105            $ret |= $offsets->getFlagsRecursively();
106        }
107        return $ret;
108    }
109
110    public function __clone() {
111        foreach ( $this->params as $k => $val ) {
112            $this->params[$k] = clone $val;
113        }
114    }
115
116    /**
117     * @return string
118     */
119    public function __toString(): string {
120        $paramBits = [];
121        foreach ( $this->params as $k => $paramOffsets ) {
122            $paramBits[] = "$k: { " . $paramOffsets->__toString() . ' }';
123        }
124        return '[ ' . implode( ', ', $paramBits ) . ' ]';
125    }
126}