Skip to content

Commit

Permalink
fix: undefined offset (#4)
Browse files Browse the repository at this point in the history
* fix: undefined offset

close swlib/saber#100

* add getLastKey

* refactor: getRedirectsTrace

* fix typo

* fix typo

* fix typo
  • Loading branch information
sy-records authored Aug 15, 2020
1 parent e946f12 commit e97d9d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Exception/TooManyRedirectsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Exception;
use Swlib\Http\Request;
use Swlib\Http\Response;
use Swlib\Http\Util;

class TooManyRedirectsException extends RequestException
{
Expand All @@ -24,7 +25,7 @@ public function __construct(
) {
$this->redirect_headers = $redirects;
$times = count($redirects);
$location = $this->redirect_headers[$times - 1];
$location = Util::getLastKey($this->redirect_headers);
$message = "Too many redirects! more than {$times} times to {$location} !";
parent::__construct($request, $response, $code, $message, $previous);
}
Expand All @@ -37,8 +38,11 @@ public function getRedirectHeaders(): array
public function getRedirectsTrace(): string
{
$trace = '';
foreach (array_keys($this->redirect_headers) as $index => $location) {
$trace .= "#$index $location\n";
$index = 0;

foreach ($this->redirect_headers as $location => $redirect_headers) {
$trace .= "#{$index} {$location}\n";
$index++;
}

return $trace;
Expand Down
12 changes: 12 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ public static function parseHeaders(string $raw_header): array
return self::parseHeader($raw_header[0]);
}
}

/**
* 获取数组最后一个键名
*
* @param array $array
* @return int|string|null
*/
public static function getLastKey(array $array)
{
end($array);
return key($array);
}
}

0 comments on commit e97d9d0

Please sign in to comment.