EntryTranslationsModel::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php namespace Anomaly\Streams\Platform\Entry;
2
3
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
4
use Anomaly\Streams\Platform\Model\EloquentModel;
5
use Carbon\Carbon;
6
7
/**
8
 * Class EntryTranslationsModel
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class EntryTranslationsModel extends EloquentModel
15
{
16
17
    /**
18
     * This model uses timestamps.
19
     *
20
     * @var bool
21
     */
22
    public $timestamps = true;
23
24
    /**
25
     * Cache minutes.
26
     *
27
     * @var int
28
     */
29
    //protected $cacheMinutes = 99999;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
31
    /**
32
     * Boot the model.
33
     */
34
    protected static function boot()
35
    {
36
        self::observe(app(substr(__CLASS__, 0, -5) . 'Observer'));
37
38
        parent::boot();
39
    }
40
41
    /**
42
     * Return the last modified datetime.
43
     *
44
     * @return Carbon
45
     */
46
    public function lastModified()
47
    {
48
        return $this->updated_at ?: $this->created_at;
0 ignored issues
show
Documentation introduced by
The property updated_at does not exist on object<Anomaly\Streams\P...EntryTranslationsModel>. 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...
Documentation introduced by
The property created_at does not exist on object<Anomaly\Streams\P...EntryTranslationsModel>. 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...
49
    }
50
51
    /**
52
     * Get the locale.
53
     *
54
     * @return string
55
     */
56
    public function getLocale()
57
    {
58
        return $this->getAttributeFromArray($this->getLocaleKey());
59
    }
60
61
    /**
62
     * Get an attribute.
63
     *
64
     * @param  string $key
65
     * @return mixed
66
     */
67
    public function getAttribute($key)
68
    {
69
        if ($key === 'locale') {
70
            return parent::getAttribute('locale');
71
        }
72
73
        if (!$parent = $this->getParent()) {
74
            return $this->attributes[$key];
75
        }
76
77
        /* @var AssignmentInterface $assignment */
78
        $assignment = $parent->getAssignment($key);
79
80
        if (!$assignment) {
81
            return parent::getAttribute($key);
82
        }
83
84
        $type = $assignment->getFieldType($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anomaly\Streams\Pla...EntryTranslationsModel>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        $type->setEntry($this);
87
        $type->setLocale($this->locale);
0 ignored issues
show
Documentation introduced by
The property locale does not exist on object<Anomaly\Streams\P...EntryTranslationsModel>. 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...
88
89
        $accessor = $type->getAccessor();
90
        $modifier = $type->getModifier();
91
92
        return $modifier->restore($accessor->get($key));
0 ignored issues
show
Unused Code introduced by
The call to FieldTypeAccessor::get() has too many arguments starting with $key.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
93
    }
94
95
    /**
96
     * Get the parent.
97
     *
98
     * @return EntryModel
99
     */
100
    public function getParent()
101
    {
102
        return isset($this->relations['parent']) ? $this->relations['parent'] : null;
103
    }
104
105
    /**
106
     * Set the attribute.
107
     *
108
     * @param string $key
109
     * @param mixed  $value
110
     */
111 View Code Duplication
    public function setAttribute($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113
        if (!$parent = $this->getParent()) {
114
            return null;
115
        }
116
117
        /* @var AssignmentInterface $assignment */
118
        $assignment = $parent->getAssignment($key);
119
120
        if (!$assignment) {
121
            parent::setAttribute($key, $value);
122
123
            return;
124
        }
125
126
        $type = $assignment->getFieldType($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anomaly\Streams\Pla...EntryTranslationsModel>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
128
        $type->setEntry($this);
129
        $type->setLocale($this->locale);
0 ignored issues
show
Documentation introduced by
The property locale does not exist on object<Anomaly\Streams\P...EntryTranslationsModel>. 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...
130
131
        $accessor = $type->getAccessor();
132
        $modifier = $type->getModifier();
133
134
        $accessor->set($modifier->modify($value));
135
    }
136
137
    /**
138
     * Fire field type events.
139
     *
140
     * @param       $trigger
141
     * @param array $payload
142
     */
143
    public function fireFieldTypeEvents($trigger, $payload = [])
144
    {
145
        if (!$parent = $this->getParent()) {
146
            return null;
147
        }
148
149
        $assignments = $parent->getAssignments();
150
151
        /* @var AssignmentInterface $assignment */
152
        foreach ($assignments->translatable() as $assignment) {
153
            $fieldType = $assignment->getFieldType();
154
155
            $fieldType->setValue($parent->getFieldValue($assignment->getFieldSlug()));
156
157
            $fieldType->setEntry($this);
158
            $fieldType->setLocale($this->locale);
0 ignored issues
show
Documentation introduced by
The property locale does not exist on object<Anomaly\Streams\P...EntryTranslationsModel>. 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...
159
160
            $fieldType->fire($trigger, array_merge(compact('fieldType', 'entry'), $payload));
161
        }
162
    }
163
164
    /**
165
     * Truncate the translation's table.
166
     *
167
     * @return mixed
168
     */
169
    public function truncate()
170
    {
171
        return $this->newQuery()->truncate();
172
    }
173
174
    /**
175
     * Let the parent handle calls if they don't exist here.
176
     *
177
     * @param  string $name
178
     * @param  array  $arguments
179
     * @return mixed
180
     */
181
    public function __call($name, $arguments)
182
    {
183
        return call_user_func_array([$this->getParent(), $name], $arguments);
184
    }
185
186
    /**
187
     * Get the attribute from the parent
188
     * if it does not exist here.
189
     *
190
     * @param  string $key
191
     * @return mixed
192
     */
193
    public function __get($key)
194
    {
195
        $value = parent::__get($key);
196
197
        if (!$value && $parent = $this->getParent()) {
198
            return $parent->{$key};
199
        }
200
201
        return $value;
202
    }
203
}
204