NewsService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getNews() 0 4 1
A getUnsetFields() 0 4 1
A getModel() 0 4 1
A getParentRelationship() 0 4 1
1
<?php
2
3
namespace FaithGen\News\Services;
4
5
use FaithGen\News\Models\News;
6
use FaithGen\SDK\Traits\FileTraits;
7
use InnoFlash\LaraStart\Services\CRUDServices;
8
9
class NewsService extends CRUDServices
10
{
11
    use FileTraits;
12
    /**
13
     * @var News
14
     */
15
    private $news;
16
17
    public function __construct(News $news)
18
    {
19
        if (request()->has('news_id')) {
20
            $this->news = News::findOrFail(request()->news_id);
21
        } else {
22
            $this->news = $news;
23
        }
24
    }
25
26
    /**
27
     * @return News
28
     */
29
    public function getNews(): News
30
    {
31
        return $this->news;
32
    }
33
34
    /**
35
     * This sets the attributes to be removed from the given set for updating or creating.
36
     * @return mixed
37
     */
38
    public function getUnsetFields()
39
    {
40
        return ['news_id', 'image'];
41
    }
42
43
    /**
44
     * This get the model value or class of the model in the service.
45
     * @return mixed
46
     */
47
    public function getModel()
48
    {
49
        return $this->getNews();
50
    }
51
52
    public function getParentRelationship()
53
    {
54
        return auth()->user()->news();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
    }
56
}
57