Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Extension_Model
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 set_threats
n/a
0 / 0
n/a
0 / 0
4
1<?php
2/**
3 * Model class for extensions.
4 *
5 * @package automattic/jetpack-protect-models
6 */
7
8namespace Automattic\Jetpack\Protect_Models;
9
10/**
11 * Model class for extension data.
12 */
13class Extension_Model {
14
15    /**
16     * The extension name.
17     *
18     * @var null|string
19     */
20    public $name;
21
22    /**
23     * The extension slug.
24     *
25     * @var null|string
26     */
27    public $slug;
28
29    /**
30     * The extension version.
31     *
32     * @var null|string
33     */
34    public $version;
35
36    /**
37     * A collection of threats related to this version of the extension.
38     *
39     * @deprecated 0.4.0 This property is deprecated. Use Threat_Model::$extension instead.
40     *
41     * @var array<Threat_Model>
42     */
43    public $threats = array();
44
45    /**
46     * Whether the extension has been checked for threats.
47     *
48     * @var null|bool
49     */
50    public $checked;
51
52    /**
53     * The type of extension ("plugins", "themes", or "core").
54     *
55     * @var null|string
56     */
57    public $type;
58
59    /**
60     * Extension Model Constructor
61     *
62     * @param array|object $extension Extension data to load into the model instance.
63     */
64    public function __construct( $extension = array() ) {
65        if ( is_object( $extension ) ) {
66            $extension = (array) $extension;
67        }
68
69        foreach ( $extension as $property => $value ) {
70            if ( property_exists( $this, $property ) ) {
71                // use the property's setter method when possible
72                if ( method_exists( $this, "set_$property" ) ) {
73                    $this->{ "set_$property" }( $value );
74                    continue;
75                }
76
77                // otherwise, map the value directly into the class property
78                $this->$property = $value;
79            }
80        }
81    }
82
83    /**
84     * Set Threats
85     *
86     * @deprecated 0.4.0 This method is deprecated. Use Threat_Model::$extension instead.
87     *
88     * @param array<Threat_Model|array|object> $threats An array of threat data to add to the extension.
89     */
90    public function set_threats( $threats ) {
91        if ( ! is_array( $threats ) ) {
92            // @phan-suppress-next-line PhanDeprecatedProperty -- Maintaining backwards compatibility.
93            $this->threats = array();
94            return;
95        }
96
97        // convert each provided threat item into an instance of Threat_Model
98        $threats = array_map(
99            function ( $threat ) {
100                if ( is_a( $threat, 'Threat_Model' ) ) {
101                    return $threat;
102                }
103
104                if ( is_object( $threat ) ) {
105                    $threat = (array) $threat;
106                }
107
108                return new Threat_Model( $threat );
109            },
110            $threats
111        );
112
113        // @phan-suppress-next-line PhanDeprecatedProperty -- Maintaining backwards compatibility.
114        $this->threats = $threats;
115    }
116}