CGIRequest   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 123
Duplicated Lines 14.63 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 18
loc 123
rs 10
c 0
b 0
f 0
wmc 25
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
C init() 18 43 13
A run() 0 11 3
A onAbort() 0 6 3
A onReadData() 0 4 1
A stdin() 0 8 2
A onWrite() 0 6 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace PHPDaemon\Applications;
3
4
use PHPDaemon\HTTPRequest\Generic;
5
6
/**
7
 * @property mixed stream
8
 * @package    Applications
9
 * @subpackage CGI
10
 *
11
 * @author     Vasily Zorin <[email protected]>
12
 */
13
class CGIRequest extends Generic
14
{
15
    /**
16
     * @var bool
17
     */
18
    public $terminateOnAbort = false;
19
    /**
20
     * @var \PHPDaemon\Core\ShellCommand
21
     */
22
    public $proc;
23
24
    /**
25
     * Constructor.
26
     * @return void
27
     */
28
    public function init()
29
    {
30
        $this->header('Content-Type: text/html'); // default header.
31
32
        $this->proc = new \PHPDaemon\Core\ShellCommand();
33
        $this->proc->readPacketSize = $this->appInstance->readPacketSize;
0 ignored issues
show
Documentation introduced by
The property readPacketSize does not exist on object<PHPDaemon\Core\ShellCommand>. Since you implemented __set, maybe consider adding a @property annotation.

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...
Documentation introduced by
The property readPacketSize does not exist on object<PHPDaemon\Core\AppInstance>. Since you implemented __set, maybe consider adding a @property annotation.

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...
34
        $this->proc->onReadData([$this, 'onReadData']);
0 ignored issues
show
Documentation Bug introduced by
The method onReadData does not exist on object<PHPDaemon\Core\ShellCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35
        $this->proc->onWrite([$this, 'onWrite']);
0 ignored issues
show
Unused Code introduced by
The call to ShellCommand::onWrite() has too many arguments starting with array($this, 'onWrite').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
36
        $this->proc->binPath = $this->appInstance->binPath;
0 ignored issues
show
Documentation introduced by
The property binPath does not exist on object<PHPDaemon\Core\AppInstance>. Since you implemented __set, maybe consider adding a @property annotation.

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...
37
        $this->proc->chroot = $this->appInstance->chroot;
0 ignored issues
show
Documentation introduced by
The property chroot does not exist on object<PHPDaemon\Core\AppInstance>. Since you implemented __set, maybe consider adding a @property annotation.

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...
38
39
        if (isset($this->attrs->server['BINPATH'])) {
40
            if (isset($this->appInstance->binAliases[$this->attrs->server['BINPATH']])) {
41
                $this->proc->binPath = $this->appInstance->binAliases[$this->attrs->server['BINPATH']];
0 ignored issues
show
Bug introduced by
The property binAliases does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
            } elseif ($this->appInstance->config->allowoverridebinpath->value) {
43
                $this->proc->binPath = $this->attrs->server['BINPATH'];
44
            }
45
        }
46
47 View Code Duplication
        if (isset($this->attrs->server['CHROOT']) && $this->appInstance->config->allowoverridechroot->value) {
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...
48
            $this->proc->chroot = $this->attrs->server['CHROOT'];
49
        }
50
51 View Code Duplication
        if (isset($this->attrs->server['SETUSER']) && $this->appInstance->config->allowoverrideuser->value) {
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...
52
            $this->proc->setUser = $this->attrs->server['SETUSER'];
53
        }
54
55 View Code Duplication
        if (isset($this->attrs->server['SETGROUP']) && $this->appInstance->config->allowoverridegroup->value) {
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...
56
            $this->proc->setGroup = $this->attrs->server['SETGROUP'];
57
        }
58
59
        if (isset($this->attrs->server['CWD']) && $this->appInstance->config->allowoverridecwd->value) {
60
            $this->proc->cwd = $this->attrs->server['CWD'];
61
        } elseif ($this->appInstance->config->cwd->value !== null) {
62
            $this->proc->cwd = $this->appInstance->config->cwd->value;
63
        } else {
64
            $this->proc->cwd = dirname($this->attrs->server['SCRIPT_FILENAME']);
65
        }
66
67
        $this->proc->setArgs([$this->attrs->server['SCRIPT_FILENAME']]);
68
        $this->proc->setEnv($this->attrs->server);
69
        $this->proc->execute();
70
    }
71
72
    /**
73
     * Called when request iterated.
74
     * @return void
75
     */
76
    public function run()
77
    {
78
        if (!$this->proc) {
79
            $this->out('Couldn\'t execute CGI proccess.');
80
            $this->finish();
81
            return;
82
        }
83
        if (!$this->proc->eof()) {
84
            $this->sleep();
85
        }
86
    }
87
88
    /**
89
     * Called when the request aborted.
90
     * @return void
91
     */
92
    public function onAbort()
93
    {
94
        if ($this->terminateOnAbort && $this->stream) {
95
            $this->stream->close();
96
        }
97
    }
98
99
    /**
100
     * Called when new data received from process.
101
     * @param object $process Process pointer.
102
     * @param string $data Data.
103
     * @return void
104
     */
105
    public function onReadData($process, $data)
106
    {
107
        $this->combinedOut($data);
108
    }
109
110
    /**
111
     * Called when new piece of request's body is received.
112
     * @param string $c Piece of request's body.
113
     * @return void
114
     */
115
    public function stdin($c)
116
    {
117
        if ($c === '') {
118
            $this->onWrite($this->proc);
119
        } else {
120
            $this->proc->write($c);
121
        }
122
    }
123
124
    /**
125
     * Called when the request aborted.
126
     * @param \PHPDaemon\Core\ShellCommand $process
127
     * @return void
128
     */
129
    public function onWrite($process)
0 ignored issues
show
Unused Code introduced by
The parameter $process is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        if ($this->attrs->stdin_done && ($this->proc->writeState === false)) {
0 ignored issues
show
Documentation introduced by
The property writeState does not exist on object<PHPDaemon\Core\ShellCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
132
            $this->proc->closeWrite();
133
        }
134
    }
135
}
136