Message   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 9 1
A editMsg() 0 10 3
1
<?php
2
3
namespace System;
4
5
class Message
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Constructor
16
     *
17
     * @param \System\Application $app
18
     */
19
    public function __construct(Application $app)
20
    {
21
        $this->app = $app;
22
    }
23
24
    /**
25
     * Get the right array from config/allow.php
26
     *
27
     * @property string $key
28
     * @param array $arguments
29
     *
30
     * @return method editMsg()
31
     */
32
    public function __call($key, $arguments)
33
    {
34
        $language = $this->app->lang->get();
0 ignored issues
show
Documentation introduced by
The property lang does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
36
        $text = $this->app->file->call('resources/languages/' . $language . '/' . $key . '.php')[$arguments[0]];
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $edit = $arguments[1] ?? [];
38
39
        return $this->editMsg($text, $edit);
40
    }
41
42
    /**
43
     * Replace the key to the value in the given array $edit
44
     *
45
     * @property string $text
46
     * @param array $edit
47
     *
48
     * @return string $text
49
     */
50
    private function editMsg($text, $edit)
51
    {
52
        if (!empty($edit)) {
53
            foreach ($edit as $key => $value) {
54
                $text = str_replace($key, $value, $text);
55
            }
56
        }
57
58
        return $text;
59
    }
60
}
61