Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

SortTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 49
ccs 0
cts 10
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sortByUpdated() 0 4 1
B sortByTag() 0 23 5
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
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 Tinyissue\Model\Traits\Project;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Query;
16
use Tinyissue\Model\Project;
17
18
/**
19
 * SortTrait is trait class containing the methods for sorting database queries of the Project model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property int          $id
24
 *
25
 * @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and')
26
 */
27
trait SortTrait
28
{
29
    /**
30
     * Sort by updated_at column.
31
     *
32
     * @param Query\Builder $query
33
     * @param string        $order
34
     *
35
     * @return void
36
     */
37
    public function sortByUpdated(Query\Builder $query, $order = 'asc')
38
    {
39
        $query->orderBy('updated_at', $order);
40
    }
41
42
    /**
43
     * Sort by issues tag group
44
     * Note: this sort will return the collection.
45
     *
46
     * @param Eloquent\Relations\Relation $query
47
     * @param string                      $tagGroup
48
     * @param string                      $order
49
     *
50
     * @return Eloquent\Collection
51
     */
52
    public function sortByTag(Eloquent\Relations\Relation $query, $tagGroup, $order = 'asc')
53
    {
54
        // If tag group is string prefixed with tag:
55
        if (!is_numeric($tagGroup)) {
56
            $tagGroup = substr($tagGroup, strlen('tag:'));
57
        }
58
59
        $results = $query->get()
60
            ->sort(function (Project\Issue $issue1, Project\Issue $issue2) use ($tagGroup, $order) {
61
                $tag1 = $issue1->tags->where('parent.id', $tagGroup, false)->first();
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. 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...
62
                $tag2 = $issue2->tags->where('parent.id', $tagGroup, false)->first();
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Tinyissue\Model\Project\Issue>. 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...
63
                $tag1 = $tag1 ? $tag1->name : '';
64
                $tag2 = $tag2 ? $tag2->name : '';
65
66
                if ($order === 'asc') {
67
                    return strcmp($tag1, $tag2);
68
                }
69
70
                return strcmp($tag2, $tag1);
71
            });
72
73
        return $results;
74
    }
75
}
76