Test Failed
Pull Request — PHP-8.x (#504)
by
unknown
27:08
created

DynamicPropertiesTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 1
A __unset() 0 6 2
A __set() 0 15 3
A __isset() 0 8 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole\Proxy;
4
5
use Hhxsv5\LaravelS\Swoole\Server;
6
7
/**
8
 * DynamicPropertiesTrait solves the issue that PHP 8.2+ does not allow dynamic property creation.
9
 * Provides functionality for storing and accessing dynamic properties.
10
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
11
trait DynamicPropertiesTrait
12
{
13
    /** @var array Stores dynamically created properties (e.g., tables) */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
14
    protected array $dynamicProperties = [];
15
16
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
17
     * Dynamic property access
18
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
19
    public function __get($name)
20
    {
21
        // First check dynamic properties
22
        return $this->dynamicProperties[$name] ?? null;
23
    }
24
25
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
26
     * Dynamic property setter
27
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
28
    public function __set($name, $value)
29
    {
30
        // If property name ends with 'Table', store it in dynamic properties
31
        if (str_ends_with($name, Server::TABLE_PROPERTY_SUFFIX)) {
32
            $this->dynamicProperties[$name] = $value;
33
            return;
34
        }
35
36
        // For other properties, try to set directly on parent class
37
        // (Swoole's C extension classes don't have __set, so it won't recurse)
38
        try {
39
            $this->{$name} = $value;
40
        } catch (\Error $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Error $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
            // When PHP 8.2+ doesn't allow dynamic property creation, store in dynamic properties
42
            $this->dynamicProperties[$name] = $value;
43
        }
44
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
47
     * Check if property exists
48
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
49
    public function __isset($name)
50
    {
51
        if (array_key_exists($name, $this->dynamicProperties)) {
52
            return true;
53
        }
54
55
        // Check parent class properties (Swoole's C extension class properties)
56
        return property_exists($this, $name);
57
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
60
     * Unset property
61
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
62
    public function __unset($name)
63
    {
64
        unset($this->dynamicProperties[$name]);
65
66
        if (property_exists($this, $name)) {
67
            unset($this->{$name});
68
        }
69
    }
70
}
71
72