Passed
Push — master ( 323542...b3d3d7 )
by Frank
06:59 queued 02:19
created

src/PluginProperties.php (4 issues)

Labels
Severity
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace MultisiteGlobalMedia;
5
6
/**
7
 * @method string basename()
8
 * @method string dirPath()
9
 * @method string dirUrl()
10
 * @method string filePath()
11
 * @method string name()
12
 * @method string website()
13
 * @method string version()
14
 * @method string textDomain()
15
 * @method string textDomainPath()
16
 */
17
class PluginProperties implements \ArrayAccess
18
{
19
    const BASENAME = 'basename';
20
    const DIR_PATH = 'dirPath';
21
    const DIR_URL = 'dirUrl';
22
    const FILE_PATH = 'filePath';
23
    const NAME = 'name';
24
    const WEBSITE = 'website';
25
    const VERSION = 'version';
26
    const TEXT_DOMAIN = 'textDomain';
27
    const TEXT_DOMAIN_PATH = 'textDomainPath';
28
29
    /**
30
     * @var array
31
     */
32
    private $properties;
33
34
    /**
35
     * @param string $pluginFilePath
36
     */
37
    public function __construct(string $pluginFilePath)
38
    {
39
        if (\is_array($this->properties)) {
40
            return;
41
        }
42
43
        $fileData = [
44
            self::BASENAME => plugin_basename($pluginFilePath),
0 ignored issues
show
The function plugin_basename was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            self::BASENAME => /** @scrutinizer ignore-call */ plugin_basename($pluginFilePath),
Loading history...
45
            self::DIR_PATH => untrailingslashit(plugin_dir_path($pluginFilePath)),
0 ignored issues
show
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            self::DIR_PATH => untrailingslashit(/** @scrutinizer ignore-call */ plugin_dir_path($pluginFilePath)),
Loading history...
46
            self::DIR_URL => untrailingslashit(plugins_url('/', $pluginFilePath)),
0 ignored issues
show
The function plugins_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            self::DIR_URL => untrailingslashit(/** @scrutinizer ignore-call */ plugins_url('/', $pluginFilePath)),
Loading history...
47
            self::FILE_PATH => $pluginFilePath,
48
        ];
49
50
        $headerData = get_file_data(
0 ignored issues
show
The function get_file_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $headerData = /** @scrutinizer ignore-call */ get_file_data(
Loading history...
51
            $pluginFilePath,
52
            [
53
                self::NAME => 'Plugin Name',
54
                self::WEBSITE => 'Plugin URI',
55
                self::VERSION => 'Version',
56
                self::TEXT_DOMAIN => 'Text Domain',
57
                self::TEXT_DOMAIN_PATH => 'Domain Path',
58
            ]
59
        );
60
61
        $this->properties = array_map('\strval', array_merge($fileData, $headerData));
62
    }
63
64
    /**
65
     * @param string $name
66
     * @param array $args
67
     * @return string
68
     */
69
    public function __call(string $name, array $args = []): string
70
    {
71
        if (!array_key_exists($name, $this->properties)) {
72
            throw new \Error(
73
                sprintf(
74
                    'Call to undefined method %s::%s()',
75
                    __CLASS__,
76
                    $name
77
                )
78
            );
79
        }
80
81
        return $this->properties[$name];
82
    }
83
84
    /**
85
     * Checks if a property with the given name exists.
86
     *
87
     * @param string $name
88
     * @return bool
89
     */
90
    public function offsetExists($name): bool
91
    {
92
        return array_key_exists($name, $this->properties);
93
    }
94
95
    /**
96
     * Returns the value of the property with the given name.
97
     *
98
     * @param string $offset
99
     * @return mixed
100
     * @throws \OutOfRangeException If there is no property with the given name.
101
     */
102
    public function offsetGet($offset)
103
    {
104
        if (!$this->offsetExists($offset)) {
105
            throw new \OutOfRangeException("'{$offset}' is not a valid plugin property.");
106
        }
107
108
        return $this->properties[$offset];
109
    }
110
111
    /**
112
     * Disabled.
113
     *
114
     * @inheritdoc
115
     *
116
     * @throws \BadMethodCallException
117
     */
118
    public function offsetSet($offset, $value)
119
    {
120
        throw new \BadMethodCallException(
121
            __METHOD__ . ' is not allowed. ' . __CLASS__ . ' is read only.'
122
        );
123
    }
124
125
    /**
126
     * Disabled.
127
     *
128
     * @inheritdoc
129
     *
130
     * @throws \BadMethodCallException
131
     */
132
    public function offsetUnset($offset)
133
    {
134
        throw new \BadMethodCallException(
135
            __METHOD__ . ' is not allowed. ' . __CLASS__ . ' is read only.'
136
        );
137
    }
138
}
139