AdvertItem::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/advert.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Advert\Models;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Kalnoy\Nestedset\NodeTrait;
16
17
class AdvertItem extends Model
18
{
19
    use NodeTrait;
20
21
    const STATUS_OPEN = 1; // 可用状态
22
23
    const STATUS_CLOSE = 0;  // 关闭状态
24
25
    protected $guarded = ['id'];
26
27
    /**
28
     * Address constructor.
29
     *
30
     * @param array $attributes
31
     */
32 4
    public function __construct(array $attributes = [])
33
    {
34 4
        $this->setTable(config('ibrand.app.database.prefix', 'ibrand_').'advert_item');
35
36 4
        parent::__construct($attributes);
37 4
    }
38
39 3
    public function advert()
40
    {
41 3
        return $this->belongsTo(Advert::class);
42
    }
43
44 3
    public function associate()
45
    {
46 3
        return $this->morphTo();
47
    }
48
49 1
    public function addChildren(array $attributes = []){
50
51 1
        $attributes['advert_id'] = $this->advert_id;
0 ignored issues
show
Documentation introduced by
The property advert_id does not exist on object<iBrand\Component\Advert\Models\AdvertItem>. 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...
52
53 1
        $attributes['parent_id'] = $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<iBrand\Component\Advert\Models\AdvertItem>. 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...
54
55 1
        return AdvertItem::create($attributes);
56
57
    }
58
59
}
60