Completed
Pull Request — master (#14)
by Harry
03:55
created

FileNode::setFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Node;
15
16
use Graze\DataFile\Format\FormatAwareInterface;
17
use Graze\DataFile\Format\FormatAwareTrait;
18
use Graze\DataFile\Modify\Compress\CompressionAwareInterface;
19
use Graze\DataFile\Modify\Compress\CompressionAwareTrait;
20
use Graze\DataFile\Modify\Encoding\EncodingAwareInterface;
21
use Graze\DataFile\Modify\Encoding\EncodingAwareTrait;
22
use Graze\DataFile\Modify\Exception\CopyFailedException;
23
use Graze\DataFile\Node\FileSystem\FilesystemWrapper;
24
use Graze\DataFile\Node\FileSystem\FileSystemWrapperInterface;
25
use League\Flysystem\File;
26
use League\Flysystem\FilesystemInterface;
27
28
class FileNode extends File implements FileNodeInterface, FormatAwareInterface, CompressionAwareInterface, EncodingAwareInterface
29
{
30
    use FormatAwareTrait;
31
    use CompressionAwareTrait;
32
    use EncodingAwareTrait;
33
34
    /** @var FilesystemWrapper */
35
    private $wrapper;
36
37
    /**
38
     * FileNode constructor.
39
     *
40
     * @param FilesystemInterface $filesystem
41
     * @param null|string         $path
42
     */
43 113
    public function __construct(FilesystemInterface $filesystem, $path)
44
    {
45 113
        $this->wrapper = new FilesystemWrapper($filesystem);
46 113
        parent::__construct($this->wrapper, $path);
47 113
    }
48
49
    /**
50
     * @return string
51
     */
52 14
    public function __toString()
53
    {
54 14
        return $this->getPath();
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60 10
    public function getDirectory()
61
    {
62 10
        return pathinfo($this->path, PATHINFO_DIRNAME) . '/';
63
    }
64
65
    /**
66
     * @return string
67
     */
68 18
    public function getFilename()
69
    {
70 18
        return pathinfo($this->path, PATHINFO_BASENAME);
71
    }
72
73
    /**
74
     * Returns the contents of the file as an array.
75
     *
76
     * @return array
77
     */
78 21
    public function getContents()
79
    {
80 21
        if ($this->exists()) {
81 19
            return explode("\n", trim($this->read()));
82
        } else {
83 2
            return [];
84
        }
85
    }
86
87
    /**
88
     * @param string|null $newPath
89
     *
90
     * @return static
91
     * @throws CopyFailedException When it is unable to copy the file
92
     */
93 4
    public function copy($newPath = null)
94
    {
95 4
        if (is_null($newPath)) {
96 1
            $newPath = $this->path . '-copy';
97 1
        }
98
99 4
        if (@$this->filesystem->copy($this->path, $newPath)) {
100 3
            return $this->getClone()->setPath($newPath);
101
        } else {
102 1
            $lastError = error_get_last();
103 1
            throw new CopyFailedException($this, $newPath, $lastError['message']);
104
        }
105
    }
106
107
    /**
108
     * @return FileSystemWrapperInterface
109
     */
110 17
    public function getFilesystem()
111
    {
112 17
        return $this->wrapper;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->wrapper; (Graze\DataFile\Node\FileSystem\FilesystemWrapper) is incompatible with the return type declared by the interface Graze\DataFile\Node\File...nterface::getFilesystem of type Graze\DataFile\Node\FilesystemWrapperInterface.

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...
113
    }
114
115
    /**
116
     * @param FilesystemInterface $filesystem
117
     *
118
     * @return $this
119
     */
120 1
    public function setFilesystem(FilesystemInterface $filesystem)
121
    {
122 1
        $this->wrapper = new FilesystemWrapper($filesystem);
123 1
        return parent::setFilesystem($this->wrapper);
124
    }
125
126
    /**
127
     * Return a clone of this object
128
     *
129
     * @return static
130
     */
131 57
    public function getClone()
132
    {
133 57
        return clone $this;
134
    }
135
136
    /**
137
     * Clone sub objects
138
     */
139 57
    public function __clone()
140
    {
141 57
        if ($this->format) {
142 1
            $this->format = clone $this->format;
143 1
        }
144 57
    }
145
}
146