Passed
Push — master ( b91050...6fb96f )
by Jens
02:59
created

ImageService::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 4
loc 4
rs 10
1
<?php
2
/**
3
 * Created by jensk on 12-10-2017.
4
 */
5
6
namespace CloudControl\Cms\services;
7
8
9
use CloudControl\Cms\storage\entities\Image;
10
use CloudControl\Cms\storage\Storage;
11
12
/**
13
 * Class ImageService
14
 * Singleton
15
 * @package CloudControl\Cms\services
16
 */
17 View Code Duplication
class ImageService
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    private static $instance;
20
    /**
21
     * @var Storage
22
     */
23
    protected $storage;
24
25
    /**
26
     * ImageService constructor.
27
     */
28
    protected function __construct()
29
    {}
30
31
    /**
32
     * @return ImageService
33
     */
34
    public static function getInstance()
35
    {
36
        if (!self::$instance instanceof ImageService) {
37
            self::$instance = new ImageService();
38
        }
39
        return self::$instance;
40
    }
41
42
    /**
43
     * @param $imagePath
44
     * @return Image
45
     */
46
    public static function get($imagePath)
47
    {
48
        $instance = self::getInstance();
49
        return $instance->getImageByPath($imagePath);
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function __toString()
56
    {
57
        return (string) print_r(self::$instance, true);
58
    }
59
60
    /**
61
     * @param $imagePath
62
     * @return Image
63
     */
64
    protected function getImageByPath($imagePath)
65
    {
66
        $image = $this->storage->getImages()->getImageByName($imagePath);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $image is correct as $this->storage->getImage...ImageByName($imagePath) (which targets CloudControl\Cms\storage...orage::getImageByName()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
        return new Image($image);
0 ignored issues
show
Documentation introduced by
$image is of type null, but the function expects a object<stdClass>.

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...
68
    }
69
70
    /**
71
     * @param Storage $storage
72
     */
73
    public function init(Storage $storage)
74
    {
75
        $this->storage = $storage;
76
    }
77
}