Completed
Push — master ( d6560c...fcbc5c )
by
unknown
06:59
created

Page   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
ccs 12
cts 18
cp 0.6667
rs 10
wmc 7
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 12 1
A findOrCreateByKey() 0 8 2
A findByKey() 0 4 1
A lines() 0 4 1
A getAll() 0 4 1
A scopeSequence() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Page extends Model
8
{
9
    public $table = 'squanto_pages';
10
11 41
    public static function make($key)
12
    {
13
        // TODO: assert unique key
14
        // TODO: sanitize and validate key
15
16 41
        $page = new self;
17 41
        $page->key = $key; // TODO: assert unique slug
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Thinktomorrow\Squanto\Domain\Page>. 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...
18 41
        $page->label = ucfirst($key);
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<Thinktomorrow\Squanto\Domain\Page>. 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...
19 41
        $page->save();
20
21 41
        return $page;
22
    }
23
24 41
    public static function findOrCreateByKey($key)
25
    {
26 41
        if ($page = self::findByKey($key)) {
27 20
            return $page;
28
        }
29
30 38
        return self::make($key);
31
    }
32
33 45
    public static function findByKey($key)
34
    {
35 45
        return self::where('key', $key)->first();
36
    }
37
38
    public function lines()
39
    {
40
        return $this->hasMany(Line::class, 'page_id');
41
    }
42
43
    public static function getAll()
44
    {
45
        return self::sequence()->get();
0 ignored issues
show
Bug introduced by
The method sequence() does not exist on Thinktomorrow\Squanto\Domain\Page. Did you maybe mean scopeSequence()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
    }
47
48
    public function scopeSequence($query)
49
    {
50
        return $query->orderBy('sequence', 'ASC');
51
    }
52
}
53