Skip to content

Commit

Permalink
fix psalm errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Jan 20, 2021
1 parent 8b24b45 commit f2e8081
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

17 changes: 17 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
findUnusedVariablesAndParams="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>

</projectFiles>
</psalm>
6 changes: 1 addition & 5 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ public static function getSchemeFrom(&$proxy)
*/
public static function httpParseHeaders($raw_headers)
{
if (function_exists('http_parse_headers')) {
http_parse_headers($raw_headers);
}
$headers = [];
$key = '';
foreach (explode("\n", $raw_headers) as $i => $h) {
foreach (explode("\n", $raw_headers) as $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if (! isset($headers[$h[0]])) {
Expand All @@ -55,7 +52,6 @@ public static function httpParseHeaders($raw_headers)
} elseif (! $key) {
$headers[0] = trim($h[0]);
}
trim($h[0]);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public function setAbortIfTooBig(int $tooBig = 2000000)
{
//$this->setOpt(CURLOPT_BUFFERSIZE, 128); // more progress info
$this->setOpt(CURLOPT_NOPROGRESS, false);
/** @psalm-suppress UnusedClosureParam */
$this->setOpt(CURLOPT_PROGRESSFUNCTION, function ($ch, $totalBytes, $receivedBytes) use ($tooBig) {
if ($receivedBytes > $tooBig) {
return 1;
Expand Down
24 changes: 18 additions & 6 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PiedWeb\Curl;

use Exception;

class Response
{
/** @var Request */
Expand All @@ -20,7 +22,7 @@ public static function get(Request $request)

$content = curl_exec($handle);

if (! $content) {
if (! $content || ! is_string($content)) {
return curl_errno($handle);
}

Expand Down Expand Up @@ -66,9 +68,19 @@ public function getContent()
*/
public function getHeaders(bool $returnArray = true)
{
if (isset($this->headers)) {
return true === $returnArray ? Helper::httpParseHeaders($this->headers) : $this->headers;
if (! $this->headers) {
return null;
}

if (! $returnArray) {
return $this->headers;
}
$parsed = Helper::httpParseHeaders($this->headers);
if ($parsed === false) {
throw new Exception('Failed to parse Headers `'.$this->headers.'`');
}

return (array) $parsed;
}

/**
Expand All @@ -92,11 +104,11 @@ public function getEffectiveUrl(): ?string
/**
* Return the cookie(s) returned by the request (if there are).
*
* @return array|null containing the cookies
* @return string|null containing the cookies
*/
public function getCookies()
{
if (isset($this->headers)) {
if ($this->headers) {
$headers = $this->getHeaders();
if (isset($headers['Set-Cookie'])) {
if (is_array($headers['Set-Cookie'])) {
Expand All @@ -113,7 +125,7 @@ public function getCookies()
*
* @param string $key to get
*
* @return string|array
* @return string|array|null
*/
public function getInfo(?string $key = null)
{
Expand Down
11 changes: 5 additions & 6 deletions src/ResponseFromCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
$content = file_exists($filePathOrContent) ? file_get_contents($filePathOrContent) : $filePathOrContent;

if (! $content) {
throw new \Exception($filePath.' doesn\'t exist');
throw new \Exception($filePathOrContent.' doesn\'t exist');
}

if (false !== $headers) {
Expand All @@ -43,26 +43,25 @@ public function getUrl()
return $this->url;
}

public function getEffectiveUrl()
public function getEffectiveUrl(): ?string
{
return $this->url;
}

public function getStatusCode()
{
if (isset($this->headers)) {
if ($this->headers) {
$headers = $this->getHeaders();
list($http, $status) = explode(' ', $headers[0], 2);

return $status;
return explode(' ', $headers[0], 2)[1];
}

return $this->getInfo('http_code');
}

public function getContentType()
{
if (isset($this->headers)) {
if ($this->headers) {
$headers = $this->getHeaders();
if (isset($headers['content-type'])) {
return $headers['content-type'];
Expand Down

0 comments on commit f2e8081

Please sign in to comment.