Test Setup Failed
Push — master ( 4a93bf...5a5f5f )
by Daniel
23:25
created

AbstractNode::copy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.9332
cc 4
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Files;
26
27
use OCA\CMSPico\Service\MiscService;
28
use OCP\Constants;
29
use OCP\Files\InvalidPathException;
30
31
abstract class AbstractNode implements NodeInterface
32
{
33
	/** @var MiscService */
34
	protected $miscService;
35
36
	/**
37
	 * AbstractNode constructor.
38
	 */
39 27
	public function __construct()
40
	{
41 27
		$this->miscService = \OC::$server->query(MiscService::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like OC::server->query(OCA\CM...ice\MiscService::class) can also be of type stdClass. However, the property $miscService is declared as type OCA\CMSPico\Service\MiscService. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42 27
	}
43
44
	/**
45
	 * {@inheritDoc}
46
	 */
47 11
	public function copy(FolderInterface $targetPath, string $name = null): NodeInterface
48
	{
49 11
		if ($name !== null) {
50 2
			$this->assertValidFileName($name);
51
		}
52
53 11
		if ($this->isFolder()) {
54
			/** @var FolderInterface $this */
55 8
			$target = $targetPath->newFolder($name ?? $this->getName());
56 8
			foreach ($this as $child) {
57 8
				$child->copy($target);
58
			}
59
		} else {
60
			/** @var FileInterface $this */
61 11
			$target = $targetPath->newFile($name ?? $this->getName());
62 11
			$target->putContent($this->getContent());
63
		}
64
65 11
		return $target;
66
	}
67
68
	/**
69
	 * {@inheritDoc}
70
	 */
71
	public function move(FolderInterface $targetPath, string $name = null): NodeInterface
72
	{
73
		if ($name !== null) {
74
			$this->assertValidFileName($name);
75
		}
76
77
		if ($this->isFolder()) {
78
			/** @var FolderInterface $this */
79
			$target = $targetPath->newFolder($name ?? $this->getName());
80
			foreach ($this as $child) {
81
				$child->move($target);
82
			}
83
		} else {
84
			/** @var FileInterface $this */
85
			$target = $targetPath->newFile($name ?? $this->getName());
86
			$target->putContent($this->getContent());
87
		}
88
89
		$this->delete();
90
		return $target;
91
	}
92
93
	/**
94
	 * {@inheritDoc}
95
	 */
96
	public function truncate(): void
97
	{
98
		if ($this->isFolder()) {
99
			/** @var FolderInterface $this */
100
			foreach ($this as $child) {
101
				$child->delete();
102
			}
103
		} else {
104
			/** @var FileInterface $this */
105
			$this->putContent('');
106
		}
107
	}
108
109
	/**
110
	 * {@inheritDoc}
111
	 */
112
	public function isFile(): bool
113
	{
114
		return ($this instanceof FileInterface);
115
	}
116
117
	/**
118
	 * {@inheritDoc}
119
	 */
120 3
	public function isFolder(): bool
121
	{
122 3
		return ($this instanceof FolderInterface);
123
	}
124
125
	/**
126
	 * {@inheritDoc}
127
	 */
128 11
	public function isReadable(): bool
129
	{
130 11
		return ($this->getPermissions() & Constants::PERMISSION_READ) === Constants::PERMISSION_READ;
131
	}
132
133
	/**
134
	 * {@inheritDoc}
135
	 */
136 7
	public function isUpdateable(): bool
137
	{
138 7
		return ($this->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE;
139
	}
140
141
	/**
142
	 * {@inheritDoc}
143
	 */
144 4
	public function isDeletable(): bool
145
	{
146 4
		return ($this->getPermissions() & Constants::PERMISSION_DELETE) === Constants::PERMISSION_DELETE;
147
	}
148
149
	/**
150
	 * {@inheritDoc}
151
	 */
152
	public function __toString(): string
153
	{
154
		return $this->getName();
155
	}
156
157
	/**
158
	 * @param string $name
159
	 *
160
	 * @throws InvalidPathException
161
	 */
162 2
	protected function assertValidFileName(string $name): void
163
	{
164 2
		if (in_array($name, [ '', '.', '..' ], true)) {
165
			throw new InvalidPathException();
166
		}
167 2
		if ((strpos($name, '/') !== false) || (strpos($name, '\\') !== false)) {
168
			throw new InvalidPathException();
169
		}
170 2
	}
171
172
	/**
173
	 * @param string $path
174
	 *
175
	 * @return string
176
	 * @throws InvalidPathException
177
	 */
178 27
	protected function normalizePath(string $path): string
179
	{
180 27
		return '/' . $this->miscService->normalizePath($path);
181
	}
182
}
183