File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
/**
3
 * Class File
4
 *
5
 * @filesource   File.php
6
 * @created      03.03.2017
7
 * @package      chillerlan\Filereader
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Filereader;
14
15
use chillerlan\Filereader\Drivers\FSDriverInterface;
16
17
/**
18
 * @property string $name
19
 * @property \chillerlan\Filereader\Directory $directory
20
 */
21
class File extends FSAbstract{
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $name;
27
28
	/**
29
	 * @var \chillerlan\Filereader\Directory
30
	 */
31
	protected $directory;
32
33
	/**
34
	 * File constructor.
35
	 *
36
	 * @param \chillerlan\Filereader\Drivers\FSDriverInterface $driver
37
	 * @param \chillerlan\Filereader\Directory                 $directory
38
	 * @param string                                           $name
39
	 */
40
	public function __construct(FSDriverInterface $driver, Directory $directory, string $name){
41
		parent::__construct($driver);
42
43
		$this->directory  = $directory;
44
		$this->name       = $name;
45
		$this->path       = $this->directory->path.DIRECTORY_SEPARATOR.$this->name;
0 ignored issues
show
Documentation introduced by
The property $path is declared protected in chillerlan\Filereader\FSAbstract. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
	}
47
48
	/**
49
	 * @return bool
50
	 */
51
	public function exists():bool{
52
		return $this->filereader->fileExists($this->path);
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	public function content():string{
59
		return $this->filereader->fileContents($this->path);
60
	}
61
62
	/**
63
	 * @return mixed
64
	 */
65
	public function getRequire(){
66
		return $this->filereader->getRequire($this->path);
67
	}
68
69
	/**
70
	 * @param string $destination
71
	 * @param bool   $overwrite
72
	 *
73
	 * @return bool
74
	 */
75
	public function copy(string $destination, bool $overwrite = true):bool{
76
		return $this->filereader->copyFile($this->path, $destination, $overwrite);
77
	}
78
79
	/**
80
	 * @param string $newname
81
	 * @param bool   $overwrite
82
	 *
83
	 * @return \chillerlan\Filereader\File
84
	 * @throws \chillerlan\Filereader\FilereaderException
85
	 */
86
	public function rename(string $newname, bool $overwrite = true):File{
87
88 View Code Duplication
		if(!$this->filereader->rename($this->path, $newname, $overwrite)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
			throw new FilereaderException('cannot rename '.$this->path.' to '.$newname); // @codeCoverageIgnore
90
		}
91
92
		if(!$this->filereader->isFile($newname)){
93
			throw new FilereaderException('file not found: '.$newname); // @codeCoverageIgnore
94
		}
95
96
		$this->path = $newname;
97
98
		$info = pathinfo($this->path);
99
100
		$this->name  = isset($info['filename']) ? $info['filename'] : '';
101
		$this->name .= '.'.$info['extension'];
102
		$this->directory = new Directory($this->filereader, $info['dirname']);
103
104
		return $this;
105
	}
106
107
	/**
108
	 * @return bool
109
	 */
110
	public function delete():bool{
111
		return $this->filereader->deleteFile($this->path);
112
	}
113
114
}
115