Completed
Push — master ( 91f666...3f0f06 )
by Jared
01:52
created

AutoTimestamps::setAutoTimestamps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Pulsar\Traits;
4
5
use Pulsar\Event\AbstractEvent;
6
use Pulsar\Event\ModelCreating;
7
use Pulsar\Type;
8
9
/**
10
 * Installs `created_at` and `updated_at` properties on the model.
11
 */
12
trait AutoTimestamps
13
{
14
    protected function autoInitializeAutoTimestamps(): void
15
    {
16
        self::saving([static::class, 'setAutoTimestamps']);
17
    }
18
19
    protected static function autoDefinitionAutoTimestamps(): void
20
    {
21
        static::$properties['created_at'] = [
22
            'type' => Type::DATE,
23
            'validate' => 'timestamp|db_timestamp',
24
        ];
25
        static::$properties['updated_at'] = [
26
            'type' => Type::DATE,
27
            'validate' => 'timestamp|db_timestamp',
28
        ];
29
    }
30
31
    public static function setAutoTimestamps(AbstractEvent $event): void
32
    {
33
        $model = $event->getModel();
34
35
        if ($event instanceof ModelCreating) {
36
            $model->created_at = time();
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Pulsar\Model>. 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...
37
        }
38
39
        $model->updated_at = time();
0 ignored issues
show
Documentation introduced by
The property updated_at does not exist on object<Pulsar\Model>. 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...
40
    }
41
}
42