TaskController::show()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 7
Bugs 0 Features 4
Metric Value
c 7
b 0
f 4
dl 11
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Acme\Transformers\TaskTransformer;
6
use App\Task;
7
use Illuminate\Http\Request;
8
9
use App\Http\Requests;
10
use Illuminate\Support\Facades\Input;
11
use Response;
12
13
class TaskController extends ApiController
14
{
15
    protected $taskTranformer;
16
17
    function __construct(TaskTransformer $taskTransformer)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    {
19
        $this->taskTransformer = $taskTransformer;
0 ignored issues
show
Bug introduced by
The property taskTransformer does not seem to exist. Did you mean taskTranformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
20
21
        // TODO: Post test not working with auth middlerare
22
        //$this->middleware('auth.basic', ['only' => 'store']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $tasks = Task::all();
33
34
        return $this->respond($this->taskTransformer->transformCollection($tasks))->setStatusCode(200);
0 ignored issues
show
Bug introduced by
The property taskTransformer does not seem to exist. Did you mean taskTranformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
    }
36
37
    /**
38
     * Show the form for creating a new resource.
39
     *
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function create()
43
    {
44
        //
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function store()
54
    {
55
        if (!Input::get('name') or !Input::get('priority') or !Input::get('done'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
56
        {
57
            return $this->setStatusCode(422)->respondWithError('Parameters failed validation for a task');
58
        }
59
60
        Task::create(Input::all());
61
62
        return $this->respondCreated('Task successfully created.');
63
    }
64
65
    /**
66
     * Display the specified resource.
67
     *
68
     * @param  int  $id
69
     * @return \Illuminate\Http\Response
70
     */
71 View Code Duplication
    public function show($id)
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...
72
    {
73
        $task = Task::find($id);
74
75
        if (!$task)
76
        {
77
            return $this->respondNotFound('Task does not exist');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->respondNot...'Task does not exist'); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by App\Http\Controllers\TaskController::show of type Illuminate\Http\Response.

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...
78
        }
79
80
        return $this->respond($this->taskTransformer->transform($task))->setStatusCode(200);
0 ignored issues
show
Bug introduced by
The property taskTransformer does not seem to exist. Did you mean taskTranformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
    }
82
83
    /**
84
     * Show the form for editing the specified resource.
85
     *
86
     * @param  int  $id
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function edit($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        //
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @param  int  $id
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function update(Request $request, $id)
102
    {
103
        $task = Task::find($id);
104
105
        if (!$task)
106
        {
107
            return $this->respondNotFound('Task does not exist!!');
108
        }
109
110
        $task->name = $request->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
111
        $task->priority = $request->priority;
1 ignored issue
show
Bug introduced by
The property priority does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
112
        $task->done = $request->done;
1 ignored issue
show
Bug introduced by
The property done does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
113
        $task->save();
114
    }
115
116
    /**
117
     * Remove the specified resource from storage.
118
     *
119
     * @param  int  $id
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function destroy($id)
123
    {
124
        Task::destroy($id);
125
    }
126
}
127