Test Failed
Pull Request — main (#12)
by
unknown
03:55 queued 01:33
created

Base::parseArgs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
namespace Clubdeuce\Tessitura\Base;
4
5
/**
6
 * Base class for Tessitura API resources.
7
 * 
8 74
 * Provides a foundation for API resources with explicit getter methods
9
 * for better IDE support and type safety. Magic methods are still supported
10 74
 * for backward compatibility but are deprecated for common properties.
11
 * 
12
 * @package Clubdeuce\Tessitura\Base
13 74
 */
14
class Base {
15 74
    protected array $_extraArgs = [];
16 70
17
    public function __construct(array $args = [])
18 70
    {
19 33
        $this->setState($args);
20
    }
21 59
22
    protected function setState(array $args = []): void
23
    {
24
        foreach ($args as $key => $value) {
25
            $property = "_{$key}";
26 6
27
            if (property_exists($this, $property)) {
28 6
                $this->{$property} = $value;
29
            } else {
30 6
                $this->_extraArgs[$key] = $value;
31 4
            }
32
        }
33
    }
34 2
35 2
    public function __call(string $name, array $args = []): mixed
36
    {
37
        // Add deprecation warning for magic method usage
38 1
        // This helps encourage developers to use explicit getter methods
39
        $commonMethods = ['id', 'name', 'description'];
40
        if (in_array(strtolower($name), $commonMethods)) {
41 24
            $methodName = 'get' . ucfirst($name);
42
            trigger_error(
43 24
                "Magic method $name() is deprecated. Use explicit method $methodName() instead for better IDE support and type safety.",
44 24
                E_USER_DEPRECATED
45 24
            );
46
        }
47
48
        $property = "_{$name}";
49 24
50
        if (property_exists($this, $property)) {
51
            return $this->{$property};
52 9
        }
53
54 9
        if (isset($this->_extraArgs[$name])) {
55
            return $this->_extraArgs[$name];
56
        }
57
58
        return false;
59
    }
60
61
    public function parseArgs(array $args = [], array $defaults = []): array
62
    {
63
        foreach ($defaults as $key => $value) {
64
            if (!isset($args[$key])) {
65
                $args[$key] = $value;
66
            }
67
        }
68
69
        return $args;
70
    }
71
72
    public function extraArgs(): array
73
    {
74
        return $this->_extraArgs;
75
    }
76
77
    /**
78
     * Get a value from extraArgs by key
79
     *
80
     * @param string $key The key to get
81
     * @param mixed $default Default value if key doesn't exist
82
     * @return mixed The value or default
83
     */
84
    public function get(string $key, mixed $default = null): mixed
85
    {
86
        return $this->_extraArgs[$key] ?? $default;
87
    }
88
89
    /**
90
     * Get ID value if it exists
91
     *
92
     * @return int The ID value or 0 if not found
93
     */
94
    public function getId(): int
95
    {
96
        return intval($this->get('Id', $this->get('id', 0)));
97
    }
98
99
    /**
100
     * Get name value if it exists
101
     *
102
     * @return string The name value or empty string if not found
103
     */
104
    public function getName(): string
105
    {
106
        return (string)$this->get('Name', $this->get('name', ''));
107
    }
108
109
    /**
110
     * Get description value if it exists
111
     *
112
     * @return string The description value or empty string if not found
113
     */
114
    public function getDescription(): string
115
    {
116
        return (string)$this->get('Description', $this->get('description', ''));
117
    }
118
}