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

PolymorphicOneToOneTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 93.1 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 54
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testEagerloadedRelationship() 28 28 1
A testLazyloadedHasOneThrough() 26 26 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\UncachedUser;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User;
5
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6
7
class PolymorphicOneToOneTest 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 testEagerloadedRelationship()
10
    {
11
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:images:genealabslaravelmodelcachingtestsfixturesimage-images.imagable_id_inraw_2-images.imagable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\User");
12
        $tags = [
13
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesimage",
14
        ];
15
16
        $result = (new User)
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloq...ns\QueriesRelationships.

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...
17
            ->with("image")
18
            ->whereHas("image")
19
            ->first()
20
            ->image;
21
        $cachedResults = $this->cache()
22
            ->tags($tags)
23
            ->get($key)['value']
24
            ->first();
25
        $liveResults = (new UncachedUser)
26
            ->with("image")
27
            ->whereHas("image")
28
            ->first()
29
            ->image;
30
31
        $this->assertEquals($liveResults->path, $result->path);
32
        $this->assertEquals($liveResults->path, $cachedResults->path);
33
        $this->assertNotEmpty($result);
34
        $this->assertNotEmpty($cachedResults);
35
        $this->assertNotEmpty($liveResults);
36
    }
37
38 View Code Duplication
    public function testLazyloadedHasOneThrough()
39
    {
40
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:images:genealabslaravelmodelcachingtestsfixturesimage-images.imagable_id_=_2-images.imagable_id_notnull-images.imagable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\User-limit_1");
41
        $tags = [
42
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesimage",
43
        ];
44
45
        $result = (new User)
46
            ->whereHas("image")
47
            ->first()
48
            ->image;
49
        $cachedResults = $this->cache()
50
            ->tags($tags)
51
            ->get($key)['value']
52
            ->first();
53
        $liveResults = (new UncachedUser)
54
            ->whereHas("image")
55
            ->first()
56
            ->image;
57
58
        $this->assertEquals($liveResults->path, $result->path);
59
        $this->assertEquals($liveResults->path, $cachedResults->path);
60
        $this->assertNotEmpty($result);
61
        $this->assertNotEmpty($cachedResults);
62
        $this->assertNotEmpty($liveResults);
63
    }
64
}
65