PostController::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php 
2
3
namespace App\Http\Controllers;
4
5
use App\Post;
6
7
use Illuminate\Http\Request;
8
9
class PostController extends Controller{
10
11
	public function __construct(){
12
13
		$this->middleware('oauth', ['except' => ['index', 'show']]);
14
		$this->middleware('authorize:' . __CLASS__, ['except' => ['index', 'show', 'store']]);
15
	}
16
17
	public function index(){
18
19
		$posts = Post::all();
20
		return $this->success($posts, 200);
0 ignored issues
show
Bug introduced by
It seems like $posts defined by \App\Post::all() on line 19 can also be of type object<Illuminate\Database\Eloquent\Collection>; however, App\Http\Controllers\Controller::success() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
21
	}
22
23
	public function store(Request $request){
24
25
		$this->validateRequest($request);
26
27
		$post = Post::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Post. Did you maybe mean created()?

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...
28
					'title' => $request->get('title'),
29
					'content'=> $request->get('content'),
30
					'user_id' => $this->getUserId()
31
				]);
32
33
		return $this->success("The post with with id {$post->id} has been created", 201);
34
	}
35
36 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...
37
38
		$post = Post::find($id);
39
40
		if(!$post){
41
			return $this->error("The post with {$id} doesn't exist", 404);
42
		}
43
44
		return $this->success($post, 200);
45
	}
46
47
	public function update(Request $request, $id){
48
49
		$post = Post::find($id);
50
51
		if(!$post){
52
			return $this->error("The post with {$id} doesn't exist", 404);
53
		}
54
55
		$this->validateRequest($request);
56
57
		$post->title 		= $request->get('title');
58
		$post->content 		= $request->get('content');
59
		$post->user_id 		= $this->getUserId();
60
61
		$post->save();
62
63
		return $this->success("The post with with id {$post->id} has been updated", 200);
64
	}
65
66 View Code Duplication
	public function destroy($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...
67
68
		$post = Post::find($id);
69
70
		if(!$post){
71
			return $this->error("The post with {$id} doesn't exist", 404);
72
		}
73
74
		// no need to delete the comments for the current post,
75
		// since we used on delete cascase on update cascase.
76
		// $post->comments()->delete();
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
77
		$post->delete();
78
79
		return $this->success("The post with with id {$id} has been deleted along with it's comments", 200);
80
	}
81
82
	public function validateRequest(Request $request){
83
84
		$rules = [
85
			'title' => 'required', 
86
			'content' => 'required'
87
		];
88
89
		$this->validate($request, $rules);
90
	}
91
92
	public function isAuthorized(Request $request){
93
94
		$resource = "posts";
95
		$post     = Post::find($this->getArgs($request)["post_id"]);
96
97
		return $this->authorizeUser($request, $resource, $post);
0 ignored issues
show
Documentation introduced by
$resource is of type string, but the function expects a array.

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...
98
	}
99
}