Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaintednessAccessorsTrait
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 21
756
0.00% covered (danger)
0.00%
0 / 1
 getTaintednessRaw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTaintednessRaw
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getCausedByRaw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCausedByRef
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFuncCausedByRaw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCausedByRaw
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setCausedByRef
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFuncCausedByRaw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMethodLinks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMethodLinks
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getMethodLinksRef
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getVarLinks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 ensureVarLinksForArgExist
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTaintednessRef
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTaintednessRef
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clearRefData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFuncTaint
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSetFuncTaint
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRetObjs
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 addRetObjs
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 initRetObjs
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php declare( strict_types=1 );
2
3// @phan-file-suppress PhanUndeclaredProperty
4
5namespace SecurityCheckPlugin;
6
7use Phan\Language\Element\FunctionInterface;
8use Phan\Language\Element\PassByReferenceVariable;
9use Phan\Language\Element\TypedElementInterface;
10
11/**
12 * Accessors to read and write taintedness props stored inside phan objects. This trait exists to avoid duplicating
13 * dynamic property names, to have better type inference, to enable phan checks for undeclared props on the other
14 * files, to keep track of props usage etc.
15 */
16trait TaintednessAccessorsTrait {
17    protected static function getTaintednessRaw( TypedElementInterface $element ): ?Taintedness {
18        return $element->taintedness ?? null;
19    }
20
21    protected static function setTaintednessRaw( TypedElementInterface $element, Taintedness $taintedness ): void {
22        $element->taintedness = $taintedness;
23        if ( $element instanceof PassByReferenceVariable ) {
24            self::setTaintednessRef( $element->getElement(), $taintedness );
25        }
26    }
27
28    protected static function getCausedByRaw( TypedElementInterface $element ): ?CausedByLines {
29        return $element->taintedOriginalError ?? null;
30    }
31
32    protected static function getCausedByRef( TypedElementInterface $element ): ?CausedByLines {
33        return $element->taintedOriginalErrorRef ?? null;
34    }
35
36    protected static function getFuncCausedByRaw( FunctionInterface $func ): ?FunctionCausedByLines {
37        return $func->funcTaintedOriginalError ?? null;
38    }
39
40    protected static function setCausedByRaw( TypedElementInterface $element, CausedByLines $lines ): void {
41        $element->taintedOriginalError = $lines;
42        if ( $element instanceof PassByReferenceVariable ) {
43            self::setCausedByRef( $element->getElement(), $lines );
44        }
45    }
46
47    protected static function setCausedByRef( TypedElementInterface $element, CausedByLines $lines ): void {
48        $element->taintedOriginalErrorRef = $lines;
49    }
50
51    protected static function setFuncCausedByRaw( FunctionInterface $func, FunctionCausedByLines $lines ): void {
52        $func->funcTaintedOriginalError = $lines;
53    }
54
55    protected static function getMethodLinks( TypedElementInterface $element ): ?MethodLinks {
56        return $element->taintedMethodLinks ?? null;
57    }
58
59    protected static function setMethodLinks( TypedElementInterface $element, MethodLinks $links ): void {
60        $element->taintedMethodLinks = $links;
61        if ( $element instanceof PassByReferenceVariable ) {
62            $element->getElement()->taintedMethodLinksRef = $links;
63        }
64    }
65
66    protected static function getMethodLinksRef( TypedElementInterface $element ): ?MethodLinks {
67        return $element->taintedMethodLinksRef ?? null;
68    }
69
70    protected static function getVarLinks( FunctionInterface $func, int $index ): ?VarLinksMap {
71        return $func->taintedVarLinks[$index] ?? null;
72    }
73
74    protected static function ensureVarLinksForArgExist( TypedElementInterface $element, int $arg ): void {
75        $element->taintedVarLinks ??= [];
76        $element->taintedVarLinks[$arg] ??= new VarLinksMap;
77    }
78
79    protected static function getTaintednessRef( TypedElementInterface $element ): ?Taintedness {
80        return $element->taintednessRef ?? null;
81    }
82
83    protected static function setTaintednessRef( TypedElementInterface $element, Taintedness $taintedness ): void {
84        $element->taintednessRef = $taintedness;
85    }
86
87    protected static function clearRefData( TypedElementInterface $element ): void {
88        unset( $element->taintednessRef, $element->taintedMethodLinksRef, $element->taintedOriginalErrorRef );
89    }
90
91    /**
92     * Get $func's taint, or null if not set.
93     */
94    protected static function getFuncTaint( FunctionInterface $func ): ?FunctionTaintedness {
95        return $func->funcTaint ?? null;
96    }
97
98    protected static function doSetFuncTaint( FunctionInterface $func, FunctionTaintedness $funcTaint ): void {
99        $func->funcTaint = $funcTaint;
100    }
101
102    /**
103     * @return TypedElementInterface[]|null
104     */
105    protected static function getRetObjs( FunctionInterface $func ): ?array {
106        $funcNode = $func->getNode();
107        if ( !$funcNode ) {
108            // If it has no node, it won't have any returned object, so don't return null, to avoid
109            // potential recursive analysis attempts.
110            return [];
111        }
112        return $funcNode->retObjs ?? null;
113    }
114
115    /**
116     * @note These are saved in the function node so that they can be shared by all implementations, without
117     * having to check the defining FQSEN of a method and canonicalize $func for lookup.
118     * @param FunctionInterface $func
119     * @param TypedElementInterface[] $retObjs
120     * @suppress PhanUnreferencedProtectedMethod Used in TaintednessVisitor
121     */
122    protected static function addRetObjs( FunctionInterface $func, array $retObjs ): void {
123        $funcNode = $func->getNode();
124        if ( $funcNode ) {
125            $funcNode->retObjs = array_merge( $funcNode->retObjs ?? [], $retObjs );
126        }
127    }
128
129    protected static function initRetObjs( FunctionInterface $func ): void {
130        $funcNode = $func->getNode();
131        if ( $funcNode ) {
132            $funcNode->retObjs ??= [];
133        }
134    }
135
136}