mkungla /
toolshedr
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * ****************************************************************** |
||
| 4 | * Created by Marko Kungla on 09 Oct 2016 |
||
| 5 | * @package toolshedr |
||
| 6 | * Encoding UTF-8 |
||
| 7 | * File Response.php |
||
| 8 | * Code format PSR-2 and 12 |
||
| 9 | * *******************************************************************/ |
||
| 10 | |||
| 11 | namespace Toolshedr\Core; |
||
| 12 | |||
| 13 | use \Toolshedr\Interfaces\ResponseDataInterface; |
||
| 14 | use \Toolshedr\Core\{ |
||
| 15 | Headers, |
||
|
0 ignored issues
–
show
|
|||
| 16 | Request |
||
|
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
Toolshedr\Core\Request.
Let’s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let’s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
|
|||
| 17 | }; |
||
| 18 | |||
| 19 | class Response |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var int Response status code |
||
| 23 | */ |
||
| 24 | private $code; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string status message |
||
| 28 | */ |
||
| 29 | private $message; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ResponseDataInterface data to be returned |
||
| 33 | */ |
||
| 34 | private $data; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Response constructor. |
||
| 38 | */ |
||
| 39 | 4 | public function __construct() |
|
| 40 | { |
||
| 41 | 4 | $this->code = 200; |
|
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 42 | 4 | $this->message = "OK"; |
|
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
OK does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. Loading history...
|
|||
| 43 | 4 | } |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Set status code |
||
| 47 | * |
||
| 48 | * @param int $code |
||
| 49 | */ |
||
| 50 | public function setCode(int $code) |
||
| 51 | { |
||
| 52 | $this->code = $code; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set response message |
||
| 57 | * |
||
| 58 | * @param string $message |
||
| 59 | */ |
||
| 60 | public function setMessage(string $message) |
||
| 61 | { |
||
| 62 | $this->message = $message; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set response data object |
||
| 67 | * |
||
| 68 | * @param ResponseDataInterface $data |
||
| 69 | */ |
||
| 70 | public function setData(ResponseDataInterface $data) |
||
| 71 | { |
||
| 72 | $this->data = $data; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Handle Options request |
||
| 77 | * |
||
| 78 | * @param Headers $headers |
||
| 79 | */ |
||
| 80 | public function options(Headers $headers) |
||
| 81 | { |
||
| 82 | if ($headers->containsRequiredHeaders()) { |
||
| 83 | $this->setCode(202); |
||
| 84 | $this->setMessage('Good to continue!!!'); |
||
| 85 | $headers->setStatusCode(202); |
||
| 86 | } else { |
||
| 87 | $this->setCode(400); |
||
| 88 | $this->setMessage('Bad Request!'); |
||
| 89 | $headers->setStatusCode(400); |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Handle requests |
||
| 96 | * |
||
| 97 | * @param Request $request |
||
| 98 | * @param Header $headers |
||
|
0 ignored issues
–
show
Should the type for parameter
$headers not be Headers?
This check looks for It makes a suggestion as to what type it considers more descriptive. Most often this is a case of a parameter that can be null in addition to its declared types. Loading history...
|
|||
| 99 | */ |
||
| 100 | public function handle(Request &$request, Headers $headers) |
||
| 101 | { |
||
| 102 | if ($request->authenticate()) { |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 103 | // go go go go |
||
| 104 | } else { |
||
| 105 | $this->setCode(401); |
||
| 106 | $this->setMessage('Unauthorized!'); |
||
| 107 | $headers->setStatusCode(401); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Return Output object |
||
| 115 | * |
||
| 116 | * @return \stdClass |
||
| 117 | */ |
||
| 118 | public function output() |
||
| 119 | { |
||
| 120 | $output = new \stdClass(); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 121 | $output->code = $this->code; |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 122 | $output->message = $this->message; |
||
| 123 | |||
| 124 | if (is_object($this->data)) { |
||
| 125 | $output->data = $this->data; |
||
| 126 | } |
||
| 127 | |||
| 128 | return $output; |
||
| 129 | } |
||
| 130 | } |
||
| 131 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: