ImageResizeExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tetsu0o
5
 * Date: 30/12/14
6
 * Time: 02:47
7
 */
8
9
namespace Mykees\MediaBundle\Twig\Extension;
10
11
12
class ImageResizeExtension extends \Twig_Extension
13
{
14
    public $rootDir;
15
16
    public function __construct($rootDir)
17
    {
18
        $this->rootDir = $rootDir;
19
    }
20
21
22
23
    public function getFunctions()
24
    {
25
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('image' => ...e' => array('html')))); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
26
            'image' => new \Twig_Function_Method($this, 'getImage', array('is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
        ];
28
    }
29
30
    public function getImage($image, $width, $height, $options=[])
31
    {
32
        $attr = false;
33
        foreach($options as $k=>$opt)
34
        {
35
            if( $k > 1 ){
36
                $attr .= ' ';
37
            }
38
            $attr .= $k.'="'.$opt.'"';
39
        }
40
41
        $webroot = $this->rootDir.'/../web/';
42
        $info = pathinfo($image);
43
        $imageResize = $info['dirname'] . '/' . $info['filename'] . "_$width" . "x$height" . '.jpg';
44
        $fullPathImageResize = $webroot . $imageResize;
45
46
        if(file_exists($fullPathImageResize))
47
        {
48
            return '<img src="'. $imageResize .'" '. $attr .'>';
49
        } else {
50
            return false;
51
        }
52
53
    }
54
55
    public function getName()
56
    {
57
        return 'image_resize';
58
    }
59
}
60