Completed
Push — master ( e090e4...18d79e )
by Damian
02:26
created

FileWriteableCheck::check()   C

Complexity

Conditions 10
Paths 20

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 51
rs 6
cc 10
eloc 34
nc 20
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Check that the given file is writable.
5
 */
6
class FileWriteableCheck implements EnvironmentCheck
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $path;
12
13
    /**
14
     * @param string $path The full path. If a relative path, it will relative to the BASE_PATH.
15
     */
16
    public function __construct($path)
17
    {
18
        $this->path = $path;
19
    }
20
21
    /**
22
     * @inheritdoc
23
     *
24
     * @return array
25
     */
26
    public function check()
27
    {
28
        if ($this->path[0] == '/') {
29
            $filename = $this->path;
30
        } else {
31
            $filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path);
32
        }
33
        
34
        if (file_exists($filename)) {
35
            $isWriteable = is_writeable($filename);
36
        } else {
37
            $isWriteable = is_writeable(dirname($filename));
38
        }
39
        
40
        if (!$isWriteable) {
41
            if (function_exists('posix_getgroups')) {
42
                $userID = posix_geteuid();
43
                $user = posix_getpwuid($userID);
44
45
                $currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename));
46
                $currentOwner = posix_getpwuid($currentOwnerID);
47
48
                $message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is currently owned by '$currentOwner[name]'.  ";
49
50
                if ($user['name'] == $currentOwner['name']) {
51
                    $message .= "We recommend that you make the file writeable.";
52
                } else {
53
                    $groups = posix_getgroups();
54
                    $groupList = array();
55
                    foreach ($groups as $group) {
56
                        $groupInfo = posix_getgrgid($group);
57
                        if (in_array($currentOwner['name'], $groupInfo['members'])) {
58
                            $groupList[] = $groupInfo['name'];
59
                        }
60
                    }
61
                    if ($groupList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
                        $message .= "	We recommend that you make the file group-writeable and change the group to one of these groups:\n - ". implode("\n - ", $groupList)
63
                            . "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename";
64
                    } else {
65
                        $message .= "  There is no user-group that contains both the web-server user and the owner of this file.  Change the ownership of the file, create a new group, or temporarily make the file writeable by everyone during the install process.";
66
                    }
67
                }
68
            } else {
69
                $message = "The webserver user needs to be able to write to this file:\n$filename";
70
            }
71
            
72
            return array(EnvironmentCheck::ERROR, $message);
73
        }
74
75
        return array(EnvironmentCheck::OK,'');
76
    }
77
}
78