Completed
Push — master ( 804c7f...1d2ba7 )
by Dan Michael O.
01:59
created

RepresentationSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 30.23 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 13
loc 43
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 6 1
A expectRequest() 0 6 1
A it_is_initializable() 0 4 1
A it_fetches_data_when_needed() 0 6 1
A it_has_files() 13 13 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
2
3
namespace spec\Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\Bibs\Bib;
6
use Scriptotek\Alma\Bibs\File;
7
use Scriptotek\Alma\Bibs\Representations;
8
use Scriptotek\Alma\Bibs\Representation;
9
use Scriptotek\Alma\Client as AlmaClient;
10
use PhpSpec\ObjectBehavior;
11
use Prophecy\Argument;
12
use spec\Scriptotek\Alma\SpecHelper;
13
14
class RepresentationSpec extends ObjectBehavior
15
{
16
    public function let(AlmaClient $client, Bib $bib)
17
    {
18
        $bib->mms_id = 'abc';
19
        $representation_id = '123';
20
        $this->beConstructedWith($client, $bib, $representation_id);
21
    }
22
23
    protected function expectRequest($client)
24
    {
25
        $client->getJSON('/bibs/abc/representations/123')
26
            ->shouldBeCalled()
27
            ->willReturn(SpecHelper::getDummyData('representation_response.json'));
28
    }
29
30
    public function it_is_initializable()
31
    {
32
        $this->shouldHaveType(Representation::class);
33
    }
34
35
    public function it_fetches_data_when_needed(AlmaClient $client)
36
    {
37
        $this->expectRequest($client);
38
39
        $this->label->shouldBe('High resolution TIFF images');
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<spec\Scriptotek\A...ibs\RepresentationSpec>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
    }
41
42 View Code Duplication
    public function it_has_files(AlmaClient $client)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $client->getJSON('/bibs/abc/representations/123/files')
45
            ->shouldBeCalled()
46
            ->willReturn(SpecHelper::getDummyData('files_response.json'));
47
48
        $files = $this->files;
0 ignored issues
show
Documentation introduced by
The property files does not exist on object<spec\Scriptotek\A...ibs\RepresentationSpec>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
        $files->shouldHaveCount(96);
50
51
        $files->rewind();
52
        $files->valid()->shouldBe(true);
53
        $files->current()->shouldHaveType(File::class);
54
    }
55
56
}
57