Issues (1358)

modules/Psr7/Response.php (4 issues)

1
<?php
2
/**
3
 * @package    Psr7
4
 * @category   modules
5
 * @author     Nazar Mokrynskyi <[email protected]>
6
 * @license    0BSD
7
 */
8
namespace cs\modules\Psr7;
9
use
10
	cs\Request as System_request,
11
	cs\Response as System_response,
12
	Exception;
13
14
class Response {
15
	/**
16
	 * Provides output to PSR-7 response object
17
	 *
18
	 * @param \Psr\Http\Message\ResponseInterface $Psr7_response
0 ignored issues
show
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	 *
20
	 * @return \Psr\Http\Message\ResponseInterface
21
	 */
22
	public static function output_to_psr7 ($Psr7_response) {
23
		$System_response = System_response::instance();
24
		self::to_psr7_body($System_response, $Psr7_response);
0 ignored issues
show
$System_response of type cs\False_class is incompatible with the type cs\Response expected by parameter $System_response of cs\modules\Psr7\Response::to_psr7_body(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
		self::to_psr7_body(/** @scrutinizer ignore-type */ $System_response, $Psr7_response);
Loading history...
25
		$Psr7_response = self::to_psr7_headers($System_response, $Psr7_response);
0 ignored issues
show
$System_response of type cs\False_class is incompatible with the type cs\Response expected by parameter $System_response of cs\modules\Psr7\Response::to_psr7_headers(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
		$Psr7_response = self::to_psr7_headers(/** @scrutinizer ignore-type */ $System_response, $Psr7_response);
Loading history...
26
		/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
27
		return $Psr7_response
28
			->withProtocolVersion(explode('/', System_request::instance()->protocol, 2)[1])
29
			->withStatus($System_response->code);
0 ignored issues
show
Bug Best Practice introduced by
The property code does not exist on cs\False_class. Since you implemented __get, consider adding a @property annotation.
Loading history...
30
	}
31
	/**
32
	 * @param System_response                     $System_response
33
	 * @param \Psr\Http\Message\ResponseInterface $Psr7_response
34
	 */
35
	protected static function to_psr7_body ($System_response, $Psr7_response) {
36
		$body = $Psr7_response->getBody();
37
		try {
38
			if (is_resource($System_response->body_stream)) {
39
				$position = ftell($System_response->body_stream);
40
				rewind($System_response->body_stream);
41
				while (!feof($System_response->body_stream)) {
42
					$body->write(fread($System_response->body_stream, 1024));
43
				}
44
				fseek($System_response->body_stream, $position);
45
			} else {
46
				$body->write($System_response->body);
47
			}
48
		} catch (Exception $e) {
49
			// Do nothing
50
		}
51
	}
52
	/**
53
	 * @param System_response                     $System_response
54
	 * @param \Psr\Http\Message\ResponseInterface $Psr7_response
55
	 *
56
	 * @return \Psr\Http\Message\ResponseInterface $Psr7_response
57
	 */
58
	protected static function to_psr7_headers ($System_response, $Psr7_response) {
59
		foreach ($System_response->headers as $header => $values) {
60
			try {
61
				$Psr7_response = $Psr7_response->withHeader($header, $values);
62
			} catch (Exception $e) {
63
				// Do nothing
64
			}
65
		}
66
		return $Psr7_response;
67
	}
68
}
69