xezzus /
humanity
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace humanity; |
||
| 3 | |||
| 4 | class Site { |
||
| 5 | |||
| 6 | private static $js; |
||
|
0 ignored issues
–
show
|
|||
| 7 | |||
| 8 | public function __construct(){ |
||
|
0 ignored issues
–
show
__construct uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 9 | |||
| 10 | $config = (new Config)->get(); |
||
| 11 | |||
| 12 | if(!isset($_SERVER['HTTP_ACCEPT'])) $_SERVER['HTTP_ACCEPT'] = '*/*'; |
||
| 13 | $acceptSrc = explode(',',$_SERVER['HTTP_ACCEPT']); |
||
| 14 | foreach($acceptSrc as $key=>$value){ |
||
| 15 | $value = explode('/',$value); |
||
| 16 | $value[1] = explode(';',$value[1]); |
||
| 17 | $name = array_shift($value[1]); |
||
| 18 | $data = []; |
||
| 19 | foreach($value[1] as $k=>$v){ |
||
| 20 | $v = explode('=',$v); |
||
| 21 | $data[$v[0]] = $v[1]; |
||
| 22 | } |
||
| 23 | $accept[$value[0]][$name] = $data; |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$accept was never initialized. Although not strictly required by PHP, it is generally a good practice to add $accept = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. Loading history...
|
|||
| 24 | } |
||
| 25 | |||
| 26 | unset($acceptSrc,$key,$value,$name,$data,$k,$v); |
||
| 27 | |||
| 28 | $requestUri = parse_url($_SERVER['REQUEST_URI']); |
||
| 29 | if($requestUri['path'] == '/javascript.js'){ |
||
| 30 | header('Content-Type: application/javascript'); |
||
| 31 | if(isset($requestUri['query'])){ |
||
| 32 | $query = explode(':',$requestUri['query']); |
||
| 33 | (new Content)->js($query); |
||
| 34 | } |
||
| 35 | exit; |
||
|
0 ignored issues
–
show
The method
__construct() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 36 | } |
||
| 37 | |||
| 38 | header('Access-Control-Allow-Origin: *'); |
||
| 39 | header('Access-Control-Max-Age: 31556926'); |
||
| 40 | header('Access-Control-Allow-Credentials: true'); |
||
| 41 | header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT'); |
||
| 42 | |||
| 43 | if(isset($accept['application']['view'])){ |
||
| 44 | header('Content-Type: text/html'); |
||
| 45 | (new RestApi)->view(); |
||
| 46 | } else if(isset($accept['application']['widget'])){ |
||
| 47 | header('Content-Type: text/html'); |
||
| 48 | (new RestApi)->widget(); |
||
| 49 | } else if(isset($accept['application']['apps'])){ |
||
| 50 | header('Content-Type: application/json'); |
||
| 51 | $query = json_decode(file_get_contents('php://input'),1); |
||
| 52 | $result = []; |
||
| 53 | foreach($query as $method=>$params){ |
||
| 54 | $stableParams = []; |
||
| 55 | $file = $config['core']['apps'].'/'.$method.'.php'; |
||
| 56 | $func = require_once($file); |
||
| 57 | $reflection = new \ReflectionFunction($func); |
||
| 58 | foreach($reflection->getParameters() as $key=>$value){ |
||
| 59 | if(isset($params[$value->name])) $stableParams[$value->name] = $params[$value->name]; |
||
| 60 | else $stableParams[$value->name] = null; |
||
| 61 | } |
||
| 62 | $name = explode('/',$method); |
||
| 63 | $count = count($name)-1; |
||
| 64 | $app = new Application; |
||
| 65 | foreach($name as $key=>$var){ |
||
| 66 | if($count == $key) $app = call_user_method_array($var,$app,$stableParams); |
||
| 67 | else $app = $app->{$var}; |
||
| 68 | } |
||
| 69 | $result[$method] = $app; |
||
| 70 | } |
||
| 71 | die(json_encode($result)); |
||
|
0 ignored issues
–
show
The method
__construct() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 72 | } else { |
||
| 73 | (new Content)->page(); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | } |
||
| 78 | ?> |
||
|
0 ignored issues
–
show
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...
|
|||
| 79 |
This check marks private properties in classes that are never used. Those properties can be removed.