TrackTransformer::getTrackStatus()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php namespace Maqe\Qwatcher\Tracks\Transformers;
2
3
use Illuminate\Database\Eloquent\Collection;
4
use Maqe\Qwatcher\Tracks\Tracks;
5
use Maqe\Qwatcher\Tracks\Transformers\TrackTransformerInterface;
6
use Illuminate\Pagination\LengthAwarePaginator;
7
8
class TrackTransformer implements TrackTransformerInterface
9
{
10
    protected $sequentialStatus = ['succeed_at', 'failed_at', 'process_at', 'queue_at'];
11
12
    /**
13
     * Transform records in tracks collection
14
     *
15
     * @param  Tracks $tracks   The tracks collection
0 ignored issues
show
Documentation introduced by
There is no parameter named $tracks. Did you maybe mean $track?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
16
     * @return Collection           The tracks collection after transform
17
     */
18
    public function transform(Tracks $track)
19
    {
20
        $track->meta = ($track->meta) ? json_decode($track->meta) : null;
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<Maqe\Qwatcher\Tracks\Tracks>. 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...
Documentation introduced by
The property meta does not exist on object<Maqe\Qwatcher\Tracks\Tracks>. 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...
21
        $track->status = $this->getTrackStatus($track);
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Maqe\Qwatcher\Tracks\Tracks>. 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...
22
        $track->statusTime = $this->getTrackStatusTime($track);
0 ignored issues
show
Documentation introduced by
The property statusTime does not exist on object<Maqe\Qwatcher\Tracks\Tracks>. 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...
23
24
        return $track;
25
    }
26
27
    /**
28
     * Transform records in tracks collection
29
     *
30
     * @param  Collection $tracks   The tracks collection
31
     * @return Collection           The tracks collection after transform
32
     */
33
    public function transforms(Collection $tracks)
34
    {
35
        return $tracks->each(function ($track) {
36
            $track = $this->transform($track);
0 ignored issues
show
Unused Code introduced by
$track is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
        });
38
    }
39
40
    /**
41
     * Transform records in tracks collection as paginate
42
     *
43
     * @param  Collection $tracks       The tracks collection
44
     * @return LengthAwarePaginator     The tracks LengthAwarePaginator after transform
45
     */
46
    public function transformPaginator(LengthAwarePaginator $tracks)
0 ignored issues
show
Coding Style introduced by
transformPaginator uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
47
    {
48
        $queryString = [];
49
        parse_str($_SERVER['QUERY_STRING'], $queryString);
50
        $tracksCollecton = new Collection($tracks->items());
51
52
53
        return new LengthAwarePaginator($this->transforms($tracksCollecton), $tracks->total(), $tracks->perPage(), $tracks->currentPage(), ['path' => \URL::current(), 'query' => $queryString]);
54
    }
55
56
    /**
57
     * Get current Status as text, tracking by sequential of status datetime
58
     *
59
     * @param  $track        The track object
60
     * @return string
61
     */
62 View Code Duplication
    public function getTrackStatus($track)
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...
63
    {
64
        foreach ($this->sequentialStatus as $status) {
65
            if (!is_null($track->{$status})) {
66
                return substr($status, 0, -3);
67
            }
68
        }
69
70
        throw new Exception("Can't find the right column to track the Qwatcher", 1);
71
    }
72
73
    /**
74
     * Get current Status prias text, tracking by sequential of status datetime
75
     *
76
     * @param  $track        The track object
77
     * @return string
78
     */
79 View Code Duplication
    public function getTrackStatusTime($track)
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...
80
    {
81
        foreach ($this->sequentialStatus as $status) {
82
            if (!is_null($track->{$status})) {
83
                return $track->{$status};
84
            }
85
        }
86
87
        throw new Exception("Can't find the right column to track the Qwatcher", 1);
88
    }
89
}
90