FilesFinder::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Dumkaaa\BxOptimize\Finder;
4
5
use Exception;
6
7
class FilesFinder implements FinderInterface
8
{
9
    /** @var string Путь к папке, в которой будем искать файлы */
10
    protected $path;
11
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function __construct($path)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
16
    {
17
        $basePath = $_SERVER['DOCUMENT_ROOT'];
18
        $path = $basePath . '/' . trim($path, '\/');
19
        if (empty($path) || !(is_dir($path) || is_file($path)) || !is_writable($path)) {
20
            throw new Exception('Путь не может быть пустым или нет доступа');
21
        }
22
        $this->path = $path;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function findFiles($path = null)
29
    {
30
        $path = $path ?: $this->path;
31
32
        return new \RecursiveIteratorIterator(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \RecursiveIte...ectoryIterator($path)); (RecursiveIteratorIterator) is incompatible with the return type declared by the interface Dumkaaa\BxOptimize\Finde...derInterface::findFiles of type array.

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...
33
            new \RecursiveDirectoryIterator($path)
34
        );
35
    }
36
}
37