Test Failed
Pull Request — main (#12)
by Daryl
02:36
created

Base::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Clubdeuce\Tessitura\Base;
4
5
/**
6
 * Base class for Tessitura API resources.
7
 * 
8
 * Provides a foundation for API resources with explicit getter methods
9
 * for better IDE support and type safety. Magic methods are still supported
10
 * for backward compatibility but are deprecated for common properties.
11
 * 
12
 * @package Clubdeuce\Tessitura\Base
13
 */
14
class Base
15
{
16
    /**
17
     * Holds the state of the object.
18
     *
19
     * This property is used to store the values of the object's properties
20
     * that are set through the constructor or other methods.
21
     *
22
     * @var mixed[]
23
     */
24 74
    protected array $_extraArgs = [];
25
26 74
    /**
27
     * Base constructor.
28
     *
29
     * Initializes the object with the provided arguments.
30
     *
31
     * @param mixed[] $args
32
     */
33
    public function __construct(array $args = [])
34
    {
35
        $this->setState($args);
36
    }
37
38
    /**
39 74
     * Sets the state of the object based on the provided arguments.
40
     *
41 74
     * This method updates the object's properties based on the keys in the
42 70
     * provided array. If a key corresponds to a property that exists in the
43
     * object, it will be set. Otherwise, it will be stored in the `_extraArgs`
44 70
     * array for later use.
45 33
     *
46 33
     * @param mixed[] $args
47
     */
48
    protected function setState(array $args = []): void
49 59
    {
50
        foreach ($args as $key => $value) {
51
            $property = "_{$key}";
52
53
            if (property_exists($this, $property)) {
54
                $this->{$property} = $value;
55
                continue;
56
            }
57
58
            $this->_extraArgs[$key] = $value;
59
        }
60 6
    }
61
62 6
    /**
63
     * Magic method to access properties dynamically.
64 6
     *
65 4
     * @param string $name
66
     * @param mixed[] $args
67
     * @return mixed
68 2
     */
69 2
    public function __call(string $name, array $args = []): mixed
70
    {
71
        // Add deprecation warning for magic method usage
72 1
        // This helps encourage developers to use explicit getter methods
73
        $commonMethods = ['id', 'name', 'description'];
74
        if (in_array(strtolower($name), $commonMethods)) {
75
            $methodName = 'get' . ucfirst($name);
76
            trigger_error(
77
                "Magic method $name() is deprecated. Use explicit method $methodName() instead for better IDE support and type safety.",
78
                E_USER_DEPRECATED
79
            );
80
        }
81
82 24
        $property = "_{$name}";
83
84 24
        if (property_exists($this, $property)) {
85 24
            return $this->{$property};
86 24
        }
87
88
        if (isset($this->_extraArgs[$name])) {
89
            return $this->_extraArgs[$name];
90 24
        }
91
92
        return false;
93
    }
94
95
    /**
96 9
     * Parses the provided arguments and fills in defaults.
97
     *
98 9
     * @param mixed[] $args
99
     * @param mixed[] $defaults
100
     * @return mixed[]
101
     */
102
    public function parseArgs(array $args = [], array $defaults = []): array
103
    {
104
        foreach ($defaults as $key => $value) {
105
            if (!isset($args[$key])) {
106
                $args[$key] = $value;
107
            }
108
        }
109
110
        return $args;
111
    }
112
113
    /**
114
     * @return mixed[]
115
     */
116
    public function extraArgs(): array
117
    {
118
        return $this->_extraArgs;
119
    }
120
121
    /**
122
     * Get a value from extraArgs by key
123
     *
124
     * @param string $key The key to get
125
     * @param mixed $default Default value if key doesn't exist
126
     * @return mixed The value or default
127
     */
128
    public function get(string $key, mixed $default = null): mixed
129
    {
130
        return $this->_extraArgs[$key] ?? $default;
131
    }
132
133
    /**
134
     * Get ID value if it exists
135
     *
136
     * @return int The ID value or 0 if not found
137
     */
138
    public function getId(): int
139
    {
140
        return intval($this->get('Id', $this->get('id', 0)));
141
    }
142
143
    /**
144
     * Get name value if it exists
145
     *
146
     * @return string The name value or empty string if not found
147
     */
148
    public function getName(): string
149
    {
150
        return (string)$this->get('Name', $this->get('name', ''));
151
    }
152
153
    /**
154
     * Get description value if it exists
155
     *
156
     * @return string The description value or empty string if not found
157
     */
158
    public function getDescription(): string
159
    {
160
        return (string)$this->get('Description', $this->get('description', ''));
161
    }
162
}
163