Completed
Push — master ( e057b1...a64391 )
by shjchen
03:51
created

Discount   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 147
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 62.96%

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 2
dl 28
loc 147
ccs 34
cts 54
cp 0.6296
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A rules() 0 4 1
A actions() 0 4 1
A hasRules() 0 4 1
A isCouponBased() 0 4 1
A getActions() 0 4 1
A getRules() 0 4 1
A setCouponUsed() 0 3 1
A getUsestartAtAttribute() 0 8 2
A getUseendAtAttribute() 0 8 2
A getStartsAt() 0 4 1
A getEndsAt() 0 4 1
A getUsed() 0 4 1
A getUsageLimit() 0 4 1
A getUseStartTimeAttribute() 10 10 2
A getUseEndTimeAttribute() 10 10 2
A getActionTypeAttribute() 8 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of ibrand/discount.
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\Discount\Models;
13
14
use iBrand\Component\Discount\Contracts\DiscountContract;
15
use Illuminate\Database\Eloquent\Model;
16
use Illuminate\Database\Eloquent\SoftDeletes;
17
18
class Discount extends Model implements DiscountContract
19
{
20
    use SoftDeletes;
21
22
    protected $guarded = ['id', 'orderAmountLimit'];
23
24
    protected $appends = ['use_start_time', 'use_end_time', 'action_type'];
25
26
    /**
27
     * Address constructor.
28
     *
29
     * @param array $attributes
30
     */
31 14
    public function __construct(array $attributes = [])
32
    {
33 14
        $this->setTable(config('ibrand.app.database.prefix', 'ibrand_').'discount');
34
35 14
        parent::__construct($attributes);
36 14
    }
37
38 14
    public function rules()
39
    {
40 14
        return $this->hasMany(Rule::class);
41
    }
42
43 14
    public function actions()
44
    {
45 14
        return $this->hasMany(Action::class);
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51 4
    public function hasRules()
52
    {
53 4
        return 0 !== $this->rules()->count();
54
    }
55
56 1
    public function isCouponBased()
57
    {
58 1
        return $this->coupon_based;
0 ignored issues
show
Documentation introduced by
The property coupon_based does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
59
    }
60
61 3
    public function getActions()
62
    {
63 3
        return $this->actions;
0 ignored issues
show
Documentation introduced by
The property actions does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
64
    }
65
66 4
    public function getRules()
67
    {
68 4
        return $this->rules;
0 ignored issues
show
Documentation introduced by
The property rules does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
69
    }
70
71 1
    public function setCouponUsed()
72
    {
73 1
    }
74
75 2
    public function getUsestartAtAttribute($value)
76
    {
77 2
        if (empty($value)) {
78 2
            return $this->starts_at;
0 ignored issues
show
Documentation introduced by
The property starts_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
79
        }
80
81 1
        return $value;
82
    }
83
84 2
    public function getUseendAtAttribute($value)
85
    {
86 2
        if (empty($value)) {
87 1
            return $this->ends_at;
0 ignored issues
show
Documentation introduced by
The property ends_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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
90 1
        return $value;
91
    }
92
93 4
    public function getStartsAt()
94
    {
95 4
        return $this->starts_at;
0 ignored issues
show
Documentation introduced by
The property starts_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
96
    }
97
98 4
    public function getEndsAt()
99
    {
100 4
        return $this->ends_at;
0 ignored issues
show
Documentation introduced by
The property ends_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106 4
    public function getUsed()
107
    {
108 4
        return $this->used;
0 ignored issues
show
Documentation introduced by
The property used does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114 4
    public function getUsageLimit()
115
    {
116 4
        return $this->usage_limit;
0 ignored issues
show
Documentation introduced by
The property usage_limit does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
117
    }
118
119 View Code Duplication
    public function getUseStartTimeAttribute()
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...
120
    {
121
        if (!$this->attributes['usestart_at']) {
122
            $time = $this->starts_at;
0 ignored issues
show
Documentation introduced by
The property starts_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
123
        } else {
124
            $time = $this->attributes['usestart_at'];
125
        }
126
127
        return date('Y-m-d', strtotime($time));
128
    }
129
130 View Code Duplication
    public function getUseEndTimeAttribute()
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...
131
    {
132
        if (!$this->attributes['useend_at']) {
133
            $time = $this->ends_at;
0 ignored issues
show
Documentation introduced by
The property ends_at does not exist on object<iBrand\Component\Discount\Models\Discount>. 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...
134
        } else {
135
            $time = $this->attributes['useend_at'];
136
        }
137
138
        return date('Y-m-d', strtotime($time));
139
    }
140
141
    /**
142
     * 为方便前台展示优惠券信息.
143
     *
144
     * @return array
145
     */
146
    public function getActionTypeAttribute()
147
    {
148
        $action = $this->actions()->first();
149
150
        $type = [];
151
152 View Code Duplication
        if(str_contains($action->type,'fixed')){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
153
            $type['type'] = 'cash';
154
            $type['value'] = json_decode($action->configuration, true)['amount'] / 100;
155
        }
156
157 View Code Duplication
        if(str_contains($action->type,'percentage')){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
158
            $type['type'] = 'percentage';
159
            $type['value'] = json_decode($action->configuration, true)['percentage'] / 100;
160
        }
161
162
        return $type;
163
    }
164
}
165