PostDetailViewModel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A setTitle() 0 6 2
A setAction() 0 6 2
A setMethod() 0 6 2
1
<?php
2
3
namespace App\ViewModels;
4
5
use App\Models\Tag;
6
use App\Models\Post;
7
use App\Models\Category;
8
use Illuminate\Support\Carbon;
9
10
class PostDetailViewModel extends FormViewModel
11
{
12
    public function __construct(Post $post)
13
    {
14
        parent::__construct($post);
15
16
        $this->published_at = $this->model->exists
0 ignored issues
show
Bug introduced by
The property published_at does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
            ? $this->model->published_at->format('Y-m-d\TH:i')
18
            : Carbon::now()->format('Y-m-d\TH:i');
19
20
        $this->categories = Category::select('id', 'name')->pluck('name', 'id');
0 ignored issues
show
Bug introduced by
The property categories does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
22
        $this->currentTags = $this->model->tags->pluck('name');
0 ignored issues
show
Bug introduced by
The property currentTags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
24
        $this->tags = Tag::orderBy('name')->pluck('name');
0 ignored issues
show
Bug introduced by
The property tags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    protected function setTitle()
28
    {
29
        $this->title = $this->model->exists
30
            ? "Edit Post: {$this->model->title}"
31
            : 'Create Post';
32
    }
33
34
    protected function setAction()
35
    {
36
        $this->action = $this->model->exists
37
            ? route('backend.posts.update', $this->model)
0 ignored issues
show
Documentation introduced by
$this->model is of type object<Illuminate\Database\Eloquent\Model>, 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...
38
            : route('backend.posts.store');
39
    }
40
41
    protected function setMethod()
42
    {
43
        $this->method = $this->model->exists
44
            ? 'PUT'
45
            : 'POST';
46
    }
47
}
48