Completed
Pull Request — master (#42)
by
unknown
02:40
created

Sprint::totalPullRequests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <[email protected]>
6
 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
7
 */
8
9
namespace GitScrum\Models;
10
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Database\Eloquent\SoftDeletes;
13
use Carbon\Carbon;
14
use GitScrum\Classes\Helper;
15
16
class Sprint extends Model
17
{
18
    use SoftDeletes;
19
20
    /**
21
     * The database table used by the model.
22
     *
23
     * @var string
24
     */
25
    protected $table = 'sprints';
26
27
    /**
28
     * Attributes that should be mass-assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = ['user_id', 'product_backlog_id', 'slug', 'title', 'description', 'version',
33
        'is_private', 'date_start', 'date_finish', 'state', 'color', 'position', 'closed_at', ];
34
35
    /**
36
     * The attributes excluded from the model's JSON form.
37
     *
38
     * @var array
39
     */
40
    protected $hidden = [];
41
42
    /**
43
     * The attributes that should be casted to native types.
44
     *
45
     * @var array
46
     */
47
    protected $casts = [];
48
49
    protected static function boot()
50
    {
51
        parent::boot();
52
    }
53
54
    public function productBacklog()
55
    {
56
        return $this->belongsTo(\GitScrum\Models\ProductBacklog::class, 'product_backlog_id', 'id');
57
    }
58
59
    public function branches()
60
    {
61
        return $this->hasMany(\GitScrum\Models\Branch::class, 'sprint_id', 'id');
62
    }
63
64
    public function issues()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
65
    {
66
        return $this->hasMany(\GitScrum\Models\Issue::class, 'sprint_id', 'id')
67
            ->orderby('position', 'ASC');
68
    }
69
70
    public function issuesHasUsers()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
71
    {
72
        $users = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
73
            return $issue->users;
74
        })->reject(function ($value) {
75
            return $value == null;
76
        })->flatten(1)->unique('id')->splice(0, 3);
77
78
        return $users->all();
79
    }
80
81
    public function comments()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
82
    {
83
        return $this->morphMany(\GitScrum\Models\Comment::class, 'commentable')
84
            ->orderby('created_at', 'DESC');
85
    }
86
87
    public function attachments()
88
    {
89
        return $this->morphMany(\GitScrum\Models\Attachment::class, 'attachmentable');
90
    }
91
92
    public function notes()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
93
    {
94
        return $this->morphMany(\GitScrum\Models\Note::class, 'noteable')
95
            ->orderby('position', 'ASC');
96
    }
97
98
    public function favorite()
99
    {
100
        return $this->morphOne(\GitScrum\Models\Favorite::class, 'favoriteable');
101
    }
102
103
    public function status()
104
    {
105
        return $this->hasOne(\GitScrum\Models\ConfigStatus::class, 'id', 'config_status_id');
106
    }
107
108
    public function pullrequests()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
109
    {
110
        $prs = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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...
111
            if ($branch->pullrequests->count()) {
112
                return $branch->pullrequests;
113
            }
114
        })->reject(function ($value) {
115
            return $value == null;
116
        });
117
118
        return $prs->all();
119
    }
120
121
    public function totalAdditions()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
122
    {
123
        $additions = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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...
124
            return $branch->commits;
125
        })->flatten(1)->map(function ($commit) {
126
            return $commit->files;
127
        })->flatten(1)->sum('additions');
128
129
        return $additions;
130
    }
131
132
    public function totalPullRequests()
133
    {
134
        $prs = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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...
135
            return $branch->pullrequests()->count();
136
        });
137
138
        return array_sum($prs->all());
139
    }
140
141
    public function workingDays($start = null)
142
    {
143
        $begin = strtotime(is_null($start) ? $this->attributes['date_start'] : $start);
144
        $end = strtotime($this->attributes['date_finish']);
145
        if ($begin > $end) {
146
            return 0;
147
        } else {
148
            $no_days = 0;
149
            $weekends = 0;
150
            while ($begin <= $end) {
151
                ++$no_days;
152
                $what_day = date('N', $begin);
153
                if ($what_day > 5) {
154
                    ++$weekends;
155
                }
156
                $begin += 86400;
157
            }
158
            $working_days = $no_days - $weekends;
159
160
            return $working_days;
161
        }
162
    }
163
164
    public function weeks($start = null)
165
    {
166
        $begin = Carbon::parse(is_null($start) ? $this->attributes['date_start'] : $start);
167
        $end = Carbon::parse($this->attributes['date_finish']);
168
169
        return round($begin->diffInDays($end) / 7);
170
    }
171
172
    public function getPSR2Errors()
173
    {
174
        $errors = 0;
175
        /*
176
        foreach ($this->branches as $branch) {
177
            foreach ($branch->commits as $commit) {
178
                foreach ($commit->files as $file) {
179
                    $errors += $file->filePhpcs->count();
180
                }
181
            }
182
        }
183
        */
184
        return $errors;
185
    }
186
187
    public function getEffort()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
188
    {
189
        $effort = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
190
            return $issue->configEffort;
191
        })->sum('effort');
192
193
        return $effort;
194
    }
195
196
    public function getEffortAvg()
197
    {
198
        $effort = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
199
            return $issue->configEffort;
200
        })->avg('effort');
201
202
        return round($effort, 2);
203
    }
204
205
    public function burndown()
206
    {
207
        $helper = new Helper();
208
        $total = $this->issues->count();
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
209
        $finished = (Carbon::now() > $this->attributes['date_finish']) ? Carbon::now() : $this->attributes['date_finish'];
210
        $finished = (is_null($this->attributes['closed_at'])) ? $finished : $this->attributes['closed_at'];
211
212
        $dates = $helper->arrayDateRange([$this->attributes['date_start'], $finished], $total);
213
214
        $previous = $this->attributes['date_start'];
215
        $arr = [];
216
        $arr[$previous] = $total;
217
218
        foreach ($dates as $date => $value) {
219
            $closed = $this->issues()->whereDate('closed_at', '=', $date)->get()->count();
220
            $totalPrevious = $total - $arr[$previous];
221
            $arr[$date] = $total - ($closed + $totalPrevious);
222
            $previous = $date;
223
        }
224
225
        return $arr;
226
    }
227
228
    public function activities()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
229
    {
230
        $activities = $this->issues()
231
            ->with('statuses')->get()->map(function ($issue) {
232
                return $issue->statuses;
233
            })->flatten(1)->map(function ($statuses) {
234
                return $statuses;
235
            })->sortByDesc('created_at');
236
237
        $activities->splice(15);
238
239
        return $activities->all();
240
    }
241
242
    public function issueTypes()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
243
    {
244
        $types = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
245
            return $issue->type;
246
        })->groupBy('slug')->map(function ($type) {
247
            return [
248
                'sprint' => $this->slug,
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<GitScrum\Models\Sprint>. 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...
249
                'slug' => $type->first()->slug,
250
                'title' => $type->first()->title,
251
                'color' => $type->first()->color,
252
                'total' => $type->count(), ];
253
        })->sortByDesc('total')->all();
254
255
        return $types;
256
    }
257
258
    public function issueStatus()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
259
    {
260
        $status = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
261
            return $issue->status;
262
        })->groupBy('slug')->all();
263
264
        return $status;
265
    }
266
267
    public function getVisibilityAttribute()
268
    {
269
        return $this->attributes['is_private'] ? trans('Private') : trans('Public');
270
    }
271
272
    public function getSlugAttribute()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
273
    {
274
        return isset($this->attributes['slug']) ? $this->attributes['slug'] : '';
275
    }
276
277
    public function getTimeboxAttribute()
278
    {
279
        $date_start = isset($this->attributes['date_start']) ?
280
            Carbon::parse($this->attributes['date_start'])->toDateString() : '';
281
        $date_finish = isset($this->attributes['date_finish']) ?
282
            Carbon::parse($this->attributes['date_finish'])->toDateString() : '';
283
284
        return $date_start.' '.trans('to').' '.$date_finish;
285
    }
286
}
287