Completed
Push — master ( b7f7d7...d34a48 )
by Tim
13s
created

PhpFilesystemAdapter::touch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\PhpFilesystemAdapter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Adapter;
22
23
/**
24
 * Adapter for a PHP filesystem implementation.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class PhpFilesystemAdapter implements FilesystemAdapterInterface
33
{
34
35
    /**
36
     * Creates a new directroy.
37
     *
38
     * @param string  $pathname The directory path
39
     * @param integer $mode     The mode is 0700 by default, which means the widest possible access
40
     *
41
     * @return boolean TRUE on success, else FALSE
42
     * @link http://php.net/mkdir
43
     */
44
    public function mkdir($pathname, $mode = 0700)
45
    {
46
        return mkdir($pathname, $mode, true);
47
    }
48
49
    /**
50
     * Query whether or not the passed filename exists.
51
     *
52
     * @param string $filename The filename to query
53
     *
54
     * @return boolean TRUE if the passed filename exists, else FALSE
55
     * @link http://php.net/is_file
56
     */
57
    public function isFile($filename)
58
    {
59
        return is_file($filename);
60
    }
61
62
    /**
63
     * Tells whether the filename is a directory.
64
     *
65
     * @param string $filename Path to the file
66
     *
67
     * @return TRUE if the filename exists and is a directory, else FALSE
68
     * @link http://php.net/is_dir
69
     */
70
    public function isDir($filename)
71
    {
72
        return is_dir($filename);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return is_dir($filename); (boolean) is incompatible with the return type declared by the interface TechDivision\Import\Adap...AdapterInterface::isDir of type TechDivision\Import\Adapter\TRUE.

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...
73
    }
74
75
    /**
76
     * Creates an empty file with the passed filename.
77
     *
78
     * @param string $filename The name of the file to create
79
     *
80
     * @return boolean TRUE if the file can be created, else FALSE
81
     */
82
    public function touch($filename)
83
    {
84
        return touch($filename);
85
    }
86
87
    /**
88
     * Renames a file or directory.
89
     *
90
     * @param string $oldname The old name
91
     * @param string $newname The new name
92
     *
93
     * @return boolean TRUE on success, else FALSE
94
     * @link http://php.net/rename
95
     */
96
    public function rename($oldname, $newname)
97
    {
98
        return rename($oldname, $newname);
99
    }
100
101
    /**
102
     * Writes the passed data to file with the passed name.
103
     *
104
     * @param string $filename The name of the file to write the data to
105
     * @param string $data     The data to write to the file
106
     *
107
     * @return number The number of bytes written to the file
108
     * @link http://php.net/file_put_contents
109
     */
110
    public function write($filename, $data)
111
    {
112
        return file_put_contents($filename, $data);
113
    }
114
115
    /**
116
     * Copy's a file from source to destination.
117
     *
118
     * @param string $src  The source file
119
     * @param string $dest The destination file
120
     *
121
     * @return boolean TRUE on success, else FALSE
122
     * @link http://php.net/copy
123
     */
124
    public function copy($src, $dest)
125
    {
126
        return copy($src, $dest);
127
    }
128
129
    /**
130
     * List the filenames of a directory.
131
     *
132
     * @param string  $directory The directory to list
133
     * @param boolean $recursive Whether to list recursively
134
     *
135
     * @return array A list of filenames
136
     */
137
    public function listContents($directory = '', $recursive = false)
138
    {
139
140
        // parse the directory
141
        $files = glob($pattern = sprintf('%s/*', $directory), 0);
142
143
        // parse all subdirectories, if recursive parsing is wanted
144
        if ($recursive !== false) {
145
            foreach (glob(dirname($pattern). DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_BRACE) as $dir) {
146
                $files = array_merge($files, $this->listContents($dir . DIRECTORY_SEPARATOR . basename($pattern), $recursive));
147
            }
148
        }
149
150
        // return the array with the files matching the glob pattern
151
        return $files;
152
    }
153
}
154