Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
9 | class PostController extends Controller{ |
||
10 | |||
11 | public function __construct(){ |
||
16 | |||
17 | public function index(){ |
||
22 | |||
23 | public function store(Request $request){ |
||
35 | |||
36 | View Code Duplication | public function show($id){ |
|
46 | |||
47 | public function update(Request $request, $id){ |
||
65 | |||
66 | View Code Duplication | public function destroy($id){ |
|
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); |
||
98 | } |
||
99 | } |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.