Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

SoftDeletesTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 80
Duplicated Lines 60 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithTrashedIsCached() 24 24 1
A testWithoutTrashedIsCached() 0 27 1
A testonlyTrashedIsCached() 24 24 1

How to fix   Duplicated Code   

Duplicated Code

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 namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6
7
class SoftDeletesTest extends IntegrationTestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
8
{
9 View Code Duplication
    public function testWithTrashedIsCached()
10
    {
11
        $author = (new UncachedAuthor)
12
            ->first();
13
        $author->delete();
14
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-find_1-withTrashed");
15
        $tags = [
16
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
17
        ];
18
19
        $deletedAuthor = (new Author)
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloq...\Database\Query\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
20
            ->withTrashed()
21
            ->find($author->id);
22
        $cachedResults = $this
23
            ->cache()
24
            ->tags($tags)
25
            ->get($key)['value'];
26
        $deletedUncachedAuthor = (new UncachedAuthor)
27
            ->withTrashed()
28
            ->find($author->id);
29
30
        $this->assertEquals($cachedResults->toArray(), $deletedAuthor->toArray());
31
        $this->assertEquals($cachedResults->toArray(), $deletedUncachedAuthor->toArray());
32
    }
33
34
    public function testWithoutTrashedIsCached()
35
    {
36
        $author = (new UncachedAuthor)
37
            ->first();
38
        $author->delete();
39
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_{$author->id}-withoutTrashed");
40
        $tags = [
41
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
42
        ];
43
44
        $result = (new Author)
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloq...\Database\Query\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
45
            ->withoutTrashed()
46
            ->find($author->id);
47
        $cachedResult = $this
48
            ->cache()
49
            ->tags($tags)
50
            ->get($key)['value'];
51
        $uncachedResult = (new UncachedAuthor)
52
            ->withoutTrashed()
53
            ->find($author->id);
54
55
        $this->assertEquals($uncachedResult, $result);
56
        $this->assertEquals($uncachedResult, $cachedResult);
57
        $this->assertNull($result);
58
        $this->assertNull($cachedResult);
59
        $this->assertNull($uncachedResult);
60
    }
61
62 View Code Duplication
    public function testonlyTrashedIsCached()
63
    {
64
        $author = (new UncachedAuthor)
65
            ->first();
66
        $author->delete();
67
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_notnull-find_{$author->id}-onlyTrashed");
68
        $tags = [
69
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
70
        ];
71
72
        $deletedAuthor = (new Author)
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloq...\Database\Query\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

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...
73
            ->onlyTrashed()
74
            ->find($author->id);
75
        $cachedResults = $this
76
            ->cache()
77
            ->tags($tags)
78
            ->get($key)['value'];
79
        $deletedUncachedAuthor = (new UncachedAuthor)
80
            ->onlyTrashed()
81
            ->find($author->id);
82
83
        $this->assertEquals($cachedResults->toArray(), $deletedAuthor->toArray());
84
        $this->assertEquals($cachedResults->toArray(), $deletedUncachedAuthor->toArray());
85
    }
86
}
87