CreatedByAndUpdatedByObserver::creating()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
namespace App\Observers;
3
4
use App\Model\BaseModel;
5
use App\Common\Auth;
6
7
final class CreatedByAndUpdatedByObserver extends BaseObserver
8
{
9
    /**
10
     * Listen to the BaseModel creating event.
11
     *
12
     * @param  BaseModel  $model
13
     * @return void
14
     */
15
    public function creating(BaseModel $model)
16
    {
17
        $user = Auth::getUser();
18
        if (!is_null($user)) {
19
            $model->created_by = $user->id;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\Model\BaseModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
20
        }
21
    }
22
23
24
    /**
25
     * Listen to the BaseModel updating event.
26
     *
27
     * @param  BaseModel  $model
28
     * @return void
29
     */
30
    public function updating(BaseModel $model)
31
    {
32
        $user = Auth::getUser();
33
        if (!is_null($user)) {
34
            $model->updated_by = $user->id;
0 ignored issues
show
Documentation introduced by
The property updated_by does not exist on object<App\Model\BaseModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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
    }
37
}
38