Test Failed
Push — master ( faab06...754a2b )
by Justin
03:28
created

FilePermissionsInstaller::install()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 6.7272
cc 7
eloc 13
nc 7
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 22.04.2018
25
 * Time: 11:51
26
 */
27
28
class FilePermissionsInstaller extends PluginInstaller_Plugin {
29
30
	public function install(Plugin $plugin, array $install_json): bool {
31
		if (isset($install_json['chmod'])) {
32
			$files = $install_json['chmod'];
33
34
			foreach ($files as $file=>$chmod_value) {
35
				if (strpos($file, "..") !== FALSE) {
36
					throw new IllegalArgumentException("Its not allowed that chmod file path in install.json of plugin '" . $plugin->getName() . "' contains '..' in path.");
37
				}
38
39
				$file_path = ROOT_PATH . $file;
40
41
				if (!file_exists($file_path)) {
42
					//create directory
43
					throw new IllegalStateException("directory '" . htmlentities($file_path) . "' doesnt exists.");
44
				}
45
46
				if (strlen($chmod_value) != 3) {
47
					throw new IllegalArgumentException("Exception in install.json of plugin '" . $plugin->getName() . "': chmod value has to be a length of 3 characters (like 755).");
48
				}
49
50
				$chmod_value = "0" . $chmod_value;
51
52
				if(!chmod($file_path, $chmod_value)) {
0 ignored issues
show
Bug introduced by
$chmod_value of type string is incompatible with the type integer expected by parameter $mode of chmod(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
				if(!chmod($file_path, /** @scrutinizer ignore-type */ $chmod_value)) {
Loading history...
53
					throw new IllegalStateException("Cannot change file permissions of directory '". $file_path . "' (plugin: " . $plugin->getName() . ".");
54
				}
55
			}
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
56
		}
57
	}
58
59
	public function uninstall(Plugin $plugin, array $install_json): bool {
60
		//dont do anything
61
		return true;
62
	}
63
64
	public function upgrade(Plugin $plugin, array $install_json): bool {
65
		return $this->install($plugin, $install_json);
66
	}
67
}
68
69
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
70