ExampleRequest::run()   B
last analyzed

Complexity

Conditions 4
Paths 12

Size

Total Lines 117

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 117
rs 8
c 0
b 0
f 0
nc 12
nop 0

How to fix   Long Method   

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
namespace PHPDaemon\Examples;
3
4
use PHPDaemon\Core\Daemon;use PHPDaemon\HTTPRequest\Generic;
5
6
class ExampleRequest extends Generic
7
{
8
    /**
9
     * Called when request iterated.
10
     * @return integer Status.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
11
     */
12
    public function run()
13
    {
14
        $this->sessionStart();
15
        try {
16
            $this->header('Content-Type: text/html');
17
            $this->setcookie('testcookie', '1');
18
        } catch (\PHPDaemon\Request\RequestHeadersAlreadySent $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
19
        }
20
21
        $this->registerShutdownFunction(function () {
22
23
?>
24
</html>
25
<?php
26
27
        });
28
29
        if (!isset($_SESSION['counter'])) {
30
            $_SESSION['counter'] = 0;
31
        }
32
33
        ?>
34
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
35
<html xmlns="http://www.w3.org/1999/xhtml">
36
<head>
37
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
38
	<title>It works!</title>
39
</head>
40
<body>
41
<h1>It works! Be happy! ;-) </h1>
42
*Hello world!<br/>
43
Testing Error Message: <?php trigger_error('_text_of_notice_');
44
        ?>
45
<br/>Counter of requests to this Application Instance: <b><?php echo ++$this->appInstance->counter;
0 ignored issues
show
Bug introduced by
The property counter 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...
46
        ?></b>
47
<br />Counter in session: <?php echo ++$_SESSION['counter'];
48
        ?>
49
<br/>Memory usage: <?php $mem = memory_get_usage();
50
        echo($mem / 1024 / 1024);
51
        ?> MB. (<?php echo $mem;
52
        ?>)
53
<br/>Memory real usage: <?php $mem = memory_get_usage(true);
54
        echo($mem / 1024 / 1024);
55
        ?> MB. (<?php echo $mem;
56
        ?>)
57
<br/>My PID: <?php echo getmypid();
58
        ?>.
59
<?php
60
61
        $user = posix_getpwuid(posix_getuid());
62
        $group = posix_getgrgid(posix_getgid());
63
64
        ?>
65
<br/>My user/group: <?php echo $user['name'] . '/' . $group['name'];
66
        ?>
67
<?php
68
69
        $displaystate = true;
70
71
        if ($displaystate) {
72
            ?><br/><br/><b>State of workers:</b><?php $stat = \PHPDaemon\Core\Daemon::getStateOfWorkers();
73
            ?>
74
<br/>Idle: <?php echo $stat['idle'];
75
            ?>
76
<br/>Busy: <?php echo $stat['busy'];
77
            ?>
78
<br/>Total alive: <?php echo $stat['alive'];
79
            ?>
80
<br/>Shutdown: <?php echo $stat['shutdown'];
81
            ?>
82
<br/>Pre-init: <?php echo $stat['preinit'];
83
            ?>
84
<br/>Init: <?php echo $stat['init'];
85
            ?>
86
<br/>
87
<?php
88
89
        }
90
91
        ?>
92
<br/><br/>
93
<br/><br/>
94
95
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
96
        ?>" method="post" enctype="multipart/form-data">
97
	<input type="file" name="myfile"/>
98
	<input type="submit" name="submit" value="Upload"/>
99
</form>
100
<br/>
101
102
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
103
        ?>" method="post">
104
	<input type="text" name="mytext" value=""/>
105
	<input type="submit" name="submit" value="Send"/>
106
</form>
107
<pre>
108
<?php
109
110
        var_dump([
0 ignored issues
show
Security Debugging Code introduced by
var_dump(array('_GET' =>..._SERVER' => $_SERVER)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
111
            '_GET'     => $_GET,
112
            '_POST'    => $_POST,
113
            '_COOKIE'  => $_COOKIE,
114
            '_REQUEST' => $_REQUEST,
115
            '_FILES'   => $_FILES,
116
            '_SERVER'  => $_SERVER
117
         ]);
118
119
        ?></pre>
120
<br/>Request took: <?php printf('%f', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 6));
121
//echo '<!-- '. str_repeat('x',1024*1024).' --->';
122
//echo '<!-- '. str_repeat('x',1024*1024).' --->';
123
//echo '<!-- '. str_repeat('x',1024*1024).' --->';
124
?>
125
</body>
126
<?php
127
128
    }
129
130
    public function __destruct()
131
    {
132
        Daemon::log('destructed example request');
133
    }
134
}
135