Project   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 386
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 34
c 5
b 1
f 0
lcom 4
cbo 3
dl 0
loc 386
rs 9.2

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteKeyName() 0 4 1
A boot() 0 12 1
A allVisible() 0 6 1
A allHidden() 0 6 1
A allGrouped() 0 6 1
A allOrdered() 0 6 1
A orderAndGroupQuery() 0 20 3
A hasBlockNamed() 0 4 1
A hasImageNamed() 0 4 1
A hasLinkNamed() 0 4 1
A hasRelationshipNamed() 0 7 1
A id() 0 4 1
A name() 0 4 1
A type() 0 4 1
A slug() 0 4 1
A order() 0 4 1
A getProjectBlock() 0 10 3
A getProjectBlockText() 0 12 4
A getProjectImage() 0 10 3
A getProjectImageUrl() 0 10 2
A scopeWithBlocks() 0 5 1
A scopeFull() 0 6 1
A scopeOrderRelationship() 0 6 1
A generateProps() 0 9 1
1
<?php
2
3
namespace Larafolio\Models;
4
5
use Larafolio\Helpers\Sluggable;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class Project extends HasContent
9
{
10
    use Sluggable, SoftDeletes;
11
12
    /**
13
     * The table associated with the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'projects';
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'name', 'slug', 'type', 'visible', 'order',
26
    ];
27
28
    /**
29
     * Properties to always eager load.
30
     *
31
     * @var array
32
     */
33
    protected $with = ['blocks', 'images', 'links'];
34
35
    /**
36
     * The attributes that should be casted to native types.
37
     *
38
     * @var array
39
     */
40
    protected $casts = [
41
        'visible' => 'boolean',
42
    ];
43
44
    /**
45
     * Fields that are dates.
46
     *
47
     * @var array
48
     */
49
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
50
51
    /**
52
     * Get the route key for the model.
53
     *
54
     * @return string
55
     */
56
    public function getRouteKeyName()
57
    {
58
        return 'slug';
59
    }
60
61
    /**
62
     * Bootstrap model.
63
     */
64
    public static function boot()
65
    {
66
        parent::boot();
67
68
        static::creating(function (Project $project) {
69
            $project->setSlug('name');
70
        });
71
72
        static::updating(function (Project $project) {
73
            $project->setSlug('name');
74
        });
75
    }
76
77
    /**
78
     * Return all visible projects.
79
     *
80
     * @param bool $group If true, group projects by 'type'.
81
     * @param bool $order If true, order projects by 'order'.
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85
    public static function allVisible($group = true, $order = true)
86
    {
87
        $query = static::where('visible', true);
88
89
        return static::orderAndGroupQuery($query, $group, $order);
90
    }
91
92
    /**
93
     * Return all hidden projects.
94
     *
95
     * @param bool $group If true, group projects by 'type'.
96
     * @param bool $order If true, order projects by 'order'.
97
     *
98
     * @return \Illuminate\Support\Collection
99
     */
100
    public static function allHidden($group = true, $order = true)
101
    {
102
        $query = static::where('visible', false);
103
104
        return static::orderAndGroupQuery($query, $group, $order);
105
    }
106
107
    /**
108
     * Return all projects grouped by 'type'.
109
     *
110
     * @param bool $order If true, order projects by 'order'.
111
     *
112
     * @return \Illuminate\Support\Collection
113
     */
114
    public static function allGrouped($order = true)
115
    {
116
        $query = static::query();
117
118
        return static::orderAndGroupQuery($query, true, $order);
0 ignored issues
show
Documentation introduced by
$query is of type object<Illuminate\Database\Eloquent\Builder>, but the function expects a object<Larafolio\Models\Builder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
    }
120
121
    /**
122
     * Return all projects ordered by 'order'.
123
     *
124
     * @return \Illuminate\Support\Collection
125
     */
126
    public static function allOrdered()
127
    {
128
        $query = static::query();
129
130
        return static::orderAndGroupQuery($query, false, true);
0 ignored issues
show
Documentation introduced by
$query is of type object<Illuminate\Database\Eloquent\Builder>, but the function expects a object<Larafolio\Models\Builder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
    }
132
133
    /**
134
     * Order and group query, return results.
135
     *
136
     * @param Builder $query Query to be ordered.
137
     * @param bool    $group If true, group projects by 'type'.
138
     * @param bool    $order If true, order projects by 'order'.
139
     *
140
     * @return \Illuminate\Support\Collection
141
     */
142
    protected static function orderAndGroupQuery($query, $group, $order)
143
    {
144
        if ($order) {
145
            $query->orderBy('order');
146
        }
147
148
        $query->orderRelationship('links');
149
150
        $query->orderRelationship('blocks');
151
152
        if ($group) {
153
            return $query->get()
154
                ->each(function ($project, $key) {
155
                    $project->index = $key;
156
                })
157
                ->groupBy('type');
158
        }
159
160
        return $query->get();
161
    }
162
163
    /**
164
     * Get all projects with given block name.
165
     *
166
     * @param string $blockName Name of block.
167
     *
168
     * @return \Illuminate\Support\Collection
169
     */
170
    public static function hasBlockNamed($blockName)
171
    {
172
        return static::hasRelationshipNamed('text_blocks', $blockName);
173
    }
174
175
    /**
176
     * Get all projects with given image name.
177
     *
178
     * @param string $imageName Name of image.
179
     *
180
     * @return \Illuminate\Support\Collection
181
     */
182
    public static function hasImageNamed($imageName)
183
    {
184
        return static::hasRelationshipNamed('images', $imageName);
185
    }
186
187
    /**
188
     * Get all projects with given link name.
189
     *
190
     * @param string $linkName Name of link.
191
     *
192
     * @return \Illuminate\Support\Collection
193
     */
194
    public static function hasLinkNamed($linkName)
195
    {
196
        return static::hasRelationshipNamed('links', $linkName);
197
    }
198
199
    /**
200
     * Get all projects with relationship on table that has given name.
201
     *
202
     * @param string $table Name of table relationship is on.
203
     * @param string $name  Relationship name.
204
     *
205
     * @return \Illuminate\Support\Collection
206
     */
207
    protected static function hasRelationshipNamed($table, $name)
208
    {
209
        return static::join($table, 'projects.id', '=', "{$table}.project_id")
210
            ->where("{$table}.name", '=', $name)
211
            ->select('projects.*')
212
            ->get();
213
    }
214
215
    /**
216
     * Return the project id.
217
     *
218
     * @return int
219
     */
220
    public function id()
221
    {
222
        return $this->id;
1 ignored issue
show
Documentation introduced by
The property id does not exist on object<Larafolio\Models\Project>. 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...
223
    }
224
225
    /**
226
     * Return the project name.
227
     *
228
     * @return string
229
     */
230
    public function name()
231
    {
232
        return $this->name;
1 ignored issue
show
Documentation introduced by
The property name does not exist on object<Larafolio\Models\Project>. 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...
233
    }
234
235
    /**
236
     * Return the project type.
237
     *
238
     * @return string
239
     */
240
    public function type()
241
    {
242
        return $this->type;
1 ignored issue
show
Documentation introduced by
The property type does not exist on object<Larafolio\Models\Project>. 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...
243
    }
244
245
    /**
246
     * Return the project slug.
247
     *
248
     * @return string
249
     */
250
    public function slug()
251
    {
252
        return $this->slug;
1 ignored issue
show
Documentation introduced by
The property slug does not exist on object<Larafolio\Models\Project>. 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...
253
    }
254
255
    /**
256
     * Return the project order value.
257
     *
258
     * @return int
259
     */
260
    public function order()
261
    {
262
        return $this->order;
1 ignored issue
show
Documentation introduced by
The property order does not exist on object<Larafolio\Models\Project>. 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...
263
    }
264
265
    /**
266
     * Get formatted text of block named description or first block.
267
     *
268
     * @return Larafolio\Models\TextBlock
269
     */
270
    public function getProjectBlock()
271
    {
272
        $block = $this->block($this->name());
273
274
        if ($block) {
275
            return $block;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $block; (Illuminate\Database\Eloquent\Model) is incompatible with the return type documented by Larafolio\Models\Project::getProjectBlock of type Larafolio\Models\Larafolio\Models\TextBlock.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
276
        } elseif ($this->hasBlocks()) {
277
            return $this->blocks()->first();
278
        }
279
    }
280
281
    /**
282
     * Get formatted text of block named description or first block.
283
     *
284
     * @param bool $formatted If true, return formatted text.
285
     *
286
     * @return string
287
     */
288
    public function getProjectBlockText($formatted = true)
289
    {
290
        $project = $this->getProjectBlock();
291
292
        if ($project && $formatted) {
293
            return $project->formattedText();
294
        } elseif ($project) {
295
            return $project->text();
296
        }
297
298
        return $project;
299
    }
300
301
    /**
302
     * Get url of small image with project name or first image in collection.
303
     *
304
     * @return Larafolio\Models\Image
305
     */
306
    public function getProjectImage()
307
    {
308
        $projectImage = $this->image($this->name());
309
310
        if ($projectImage) {
311
            return $projectImage;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $projectImage; (Illuminate\Database\Eloquent\Model) is incompatible with the return type documented by Larafolio\Models\Project::getProjectImage of type Larafolio\Models\Larafolio\Models\Image.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
312
        } elseif ($this->hasImages()) {
313
            return $this->images()->first();
314
        }
315
    }
316
317
    /**
318
     * Get url of small image with project name or first image in collection.
319
     *
320
     * @param string $size The size of the image, name of image cache filter.
321
     *
322
     * @return string
323
     */
324
    public function getProjectImageUrl($size = 'small')
325
    {
326
        $projectImage = $this->getProjectImage();
327
328
        if ($projectImage) {
329
            return $projectImage->{$size}();
330
        }
331
332
        return $projectImage;
333
    }
334
335
    /**
336
     * Get blocks sorted by order.
337
     *
338
     * @param \Builder $query Query builder.
339
     * @param string   $slug  Project slug.
340
     *
341
     * @return \Builder
342
     */
343
    public function scopeWithBlocks($query, $slug)
344
    {
345
        return $query->orderRelationship('blocks')
346
            ->where('slug', $slug);
347
    }
348
349
    /**
350
     * Get full project info (blocks and links sorted by order).
351
     *
352
     * @param \Builder $query Query builder.
353
     * @param string   $slug  Project slug.
354
     *
355
     * @return \Builder
356
     */
357
    public function scopeFull($query, $slug)
358
    {
359
        return $query->orderRelationship('blocks')
360
            ->orderRelationship('links')
361
            ->where('slug', $slug);
362
    }
363
364
    /**
365
     * Order given relationship by order value.
366
     *
367
     * @param \Builder $query        Query builder.
368
     * @param string   $relationship Name of relationship to order.
369
     *
370
     * @return \Builder
371
     */
372
    public function scopeOrderRelationship($query, $relationship)
373
    {
374
        return $query->with([$relationship => function ($query) {
375
            $query->orderBy('order');
376
        }]);
377
    }
378
379
    /**
380
     * Return project properties to be passed to js.
381
     *
382
     * @return array
383
     */
384
    public function generateProps()
385
    {
386
        return [
387
            'deletedAt' => $this->deleted_at->diffForHumans(),
1 ignored issue
show
Documentation introduced by
The property deleted_at does not exist on object<Larafolio\Models\Project>. 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...
388
            'id'        => $this->id(),
389
            'name'      => $this->name(),
390
            'slug'      => $this->slug(),
391
        ];
392
    }
393
}
394