Skip to content

Commit cea1b9a

Browse files
author
Wazabii
committed
Types
1 parent 0b0bb9d commit cea1b9a

File tree

9 files changed

+73
-70
lines changed

9 files changed

+73
-70
lines changed

Client.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,50 @@ public function sendRequest(RequestInterface $request): ResponseInterface
7474
// Init curl request
7575
$this->curl = curl_init();
7676
$this->buildOptions();
77-
switch ($request->getMethod()) {
78-
case 'GET':
79-
$this->get();
80-
break;
81-
case 'POST':
82-
$this->post();
83-
break;
84-
case 'PUT':
85-
$this->put();
86-
break;
87-
case 'PATCH':
88-
$request = $this->patch($request);
89-
break;
90-
case 'DELETE':
91-
$this->delete();
92-
break;
93-
default:
94-
throw new InvalidArgumentException('The requesr method (' . $request->getMethod() . ') ' .
95-
'is not supported.');
96-
}
77+
$this->buildFromMethods($request);
9778

9879
// Execute request
9980
$this->createRequest();
10081

10182
// Close curl request
10283
curl_close($this->curl);
84+
85+
// Retrive the body
86+
return $this->createResponse();
10387
} catch (InvalidArgumentException $e) {
10488
throw new RequestException($e->getMessage(), 1);
10589
} catch (NetworkException $e) {
10690
throw $e;
10791
}
92+
}
10893

109-
// Retrive the body
110-
return $this->createResponse();
94+
/**
95+
* Build client curl methods
96+
* @param RequestInterface $request
97+
* @return void
98+
*/
99+
protected function buildFromMethods(RequestInterface $request): void
100+
{
101+
switch ($request->getMethod()) {
102+
case 'GET':
103+
$this->get();
104+
// no break
105+
case 'POST':
106+
$this->post();
107+
// no break
108+
case 'PUT':
109+
$this->put();
110+
// no break
111+
case 'PATCH':
112+
$request = $this->patch($request);
113+
// no break
114+
case 'DELETE':
115+
$this->delete();
116+
// no break
117+
default:
118+
throw new InvalidArgumentException('The requesr method (' . $request->getMethod() . ') ' .
119+
'is not supported.');
120+
}
111121
}
112122

113123
/**
@@ -188,7 +198,7 @@ protected function put(): void
188198

189199
/**
190200
* Path request
191-
* @return void
201+
* @return RequestInterface
192202
*/
193203
protected function patch(RequestInterface $request): RequestInterface
194204
{
@@ -228,7 +238,7 @@ private function buildOptions(): void
228238
private function buildHeaders(RequestInterface $request): void
229239
{
230240
$data = array();
231-
foreach ($request->getHeaders() as $name => $val) {
241+
foreach ($request->getHeaders() as $name => $_unUsedVal) {
232242
$data[] = "{$name}: " . $request->getHeaderLine($name);
233243
}
234244
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $data);

Cookies.php

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,32 @@
88

99
class Cookies implements CookiesInterface
1010
{
11-
private $name;
12-
private $value;
13-
private $expires;
11+
//private $name;
12+
//private $value;
13+
//private $expires;
1414
private $path;
1515
private $domain;
1616
private $secure;
17-
private $httponly;
17+
private $httpOnly;
1818
private $samesite;
1919

2020
/**
21-
* [__construct description]
22-
* setcookie(
23-
string $name,
24-
string $value = "",
25-
int $expires_or_options = 0,
26-
string $path = "",
27-
string $domain = "",
28-
bool $secure = false,
29-
bool $httponly = false
30-
): bool
31-
* @param [type] $uri [description]
21+
* Set Cookie
22+
* @param string $path
23+
* @param string $domain
24+
* @param bool|boolean $secure
25+
* @param bool|boolean $httpOnly
3226
*/
3327
public function __construct(
3428
string $path = "/",
3529
string $domain = "",
3630
bool $secure = true,
37-
bool $httponly = true
31+
bool $httpOnly = true
3832
) {
3933
$this->path = $path;
4034
$this->domain = $domain;
4135
$this->secure = $secure;
42-
$this->httponly = $httponly;
36+
$this->httpOnly = $httpOnly;
4337
}
4438

4539
/**
@@ -54,7 +48,7 @@ public function setPath(string $path)
5448

5549
/**
5650
* Set cookie allowed domain
57-
* @param string $path URI Path
51+
* @param string $domain URI Path
5852
*/
5953
public function setDomain(string $domain)
6054
{
@@ -64,7 +58,7 @@ public function setDomain(string $domain)
6458

6559
/**
6660
* Set cookie secure flag (HTTPS only: true)
67-
* @param string $path URI Path
61+
* @param string $secure URI Path
6862
*/
6963
public function setSecure(bool $secure)
7064
{
@@ -75,19 +69,18 @@ public function setSecure(bool $secure)
7569
/**
7670
* Set cookie http only flag. Cookie won't be accessible by scripting languages, such as JavaScript if true.
7771
* Can effectively help to reduce identity theft through XSS attacks, Not supported in all browsers tho
78-
* @param string $path URI Path
72+
* @param bool $httpOnly enable http only flag
7973
*/
80-
public function setHttpOnly(bool $httponly)
74+
public function sethttpOnly(bool $httpOnly)
8175
{
82-
$this->httponly = $httponly;
76+
$this->httpOnly = $httpOnly;
8377
return $this;
8478
}
8579

8680

8781
/**
88-
* Set same site
89-
* (Requires PHP version >= 7.3.0)
90-
* @param string $sameSite [description]
82+
* Set same site (Requires PHP version >= 7.3.0)
83+
* @param string $samesite
9184
*/
9285
public function setSameSite(string $samesite)
9386
{
@@ -110,7 +103,7 @@ public function set(string $name, string $value, int $expires, bool $force = fal
110103
if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
111104
setcookie($name, $value, $this->cookieOpt($expires));
112105
} else {
113-
setcookie($name, $value, $expires, $this->path, $this->domain, $this->secure, $this->httponly);
106+
setcookie($name, $value, $expires, $this->path, $this->domain, $this->secure, $this->httpOnly);
114107
}
115108
if ($force) {
116109
$_COOKIE[$name] = $value;
@@ -158,7 +151,7 @@ public function delete(string $name): void
158151
*/
159152
public function isSecure(): bool
160153
{
161-
return (bool)($this->samesite === "Strict" && $this->secure && $this->httponly);
154+
return (bool)($this->samesite === "Strict" && $this->secure && $this->httpOnly);
162155
}
163156

164157
/**
@@ -173,7 +166,7 @@ private function cookieOpt(int $expires): array
173166
'path' => $this->path,
174167
'domain' => $this->domain,
175168
'secure' => $this->secure,
176-
'httponly' => $this->httponly,
169+
'httponly' => $this->httpOnly,
177170
'samesite' => $this->samesite
178171
];
179172
}

Interfaces/ClientInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ interface ClientInterface
1010
* @param RequestInterface $request
1111
*
1212
* @return ResponseInterface
13-
*
14-
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
1513
*/
1614
public function sendRequest(RequestInterface $request): ResponseInterface;
1715
}

Message.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public function hasHeader($name): bool
8585
public function getHeaderLine($name)
8686
{
8787
$data = $this->getHeaderLineData($name);
88-
return (count($data) > 0) ? implode("; ", $data) : ($data[0] ?? "");
88+
if (!is_array($data)) {
89+
throw new \Exception("The header line is not an array!", 1);
90+
}
91+
return (count($data) > 0) ? implode("; ", $data) : "";
8992
}
9093

9194
/**

Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getMethod(): string
6565

6666
/**
6767
* Return an instance with the specific set Method
68-
* @param string $requestTarget
68+
* @param string $method
6969
* @return RequestInterface
7070
*/
7171
public function withMethod(string $method): RequestInterface

Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function createHeaders(): void
223223
{
224224
if (is_null($this->hasHeadersInit)) {
225225
$this->hasHeadersInit = true;
226-
foreach ($this->getHeaders() as $key => $val) {
226+
foreach ($this->getHeaders() as $key => $_unusedVal) {
227227
$value = $this->getHeaderLine($key);
228228
header("{$key}: {$value}");
229229
}

ServerRequest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ public function getCliKeyword(): ?string
333333
if (is_null($this->cliKeywords)) {
334334
$new = array();
335335
$arg = $this->getUri()->getArgv();
336-
foreach ($arg as $k => $v) {
337-
if ((($p1 = strpos($v, "--")) === 0) || (($p2 = strpos($v, "-")) === 0)) {
336+
foreach ($arg as $v) {
337+
if ((($pos1 = strpos($v, "--")) === 0) || (($pos2 = strpos($v, "-")) === 0)) {
338338
break;
339339
} else {
340340
$new[] = $v;
@@ -356,10 +356,10 @@ public function getCliArgs(): array
356356
if (is_null($this->cliArgs)) {
357357
$arg = $this->getUri()->getArgv();
358358
$this->cliArgs = array();
359-
foreach ($arg as $k => $v) {
359+
foreach ($arg as $v) {
360360
$v = str_replace("&", "#", $v);
361-
if ((($p1 = strpos($v, "--")) === 0) || (($p2 = strpos($v, "-")) === 0)) {
362-
parse_str(substr($v, ($p1 !== false ? 2 : 1)), $result);
361+
if ((($pos1 = strpos($v, "--")) === 0) || (($pos2 = strpos($v, "-")) === 0)) {
362+
parse_str(substr($v, ($pos1 !== false ? 2 : 1)), $result);
363363
foreach ($result as &$val) {
364364
$val = str_replace("#", "&", $val);
365365
}

Stream.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class Stream implements StreamInterface
3232
private $readable;
3333
private $writable;
3434
private $seekable;
35-
private $output;
35+
//private $output;
3636

3737

3838
/**
3939
* PSR-7 Stream
40-
* @param Resource $stream
40+
* @param mixed $stream
4141
* @param string $permission Default stream permission is r+
4242
*/
43-
public function __construct($stream = null, string $permission = "r+")
43+
public function __construct(mixed $stream = null, string $permission = "r+")
4444
{
4545
if (is_null($stream)) {
4646
$stream = $this::DEFAULT_WRAPPER;
@@ -72,7 +72,7 @@ public function getStream(): string
7272
* Get contents
7373
* @return string
7474
*/
75-
public function __toString()
75+
public function __toString(): string
7676
{
7777
$this->rewind();
7878
return $this->getContents();

Uri.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function pollyfill()
7373
* Get formated URI
7474
* @return string
7575
*/
76-
public function __toString()
76+
public function __toString(): string
7777
{
7878
return $this->getUri();
7979
}
@@ -132,7 +132,6 @@ public function getUserInfo(): string
132132
{
133133
if (is_null($this->userInfo)) {
134134
$this->userInfo = "";
135-
$user = $pass = null;
136135
if ($user = $this->getUniquePart("user")) {
137136
$this->encoded['user'] = $user;
138137
}
@@ -304,7 +303,7 @@ public function withHost(string $host): UriInterface
304303

305304
/**
306305
* Create new instance with same URI, BUT with a new port
307-
* @param int $post
306+
* @param int $port
308307
* @return UriInterface
309308
*/
310309
public function withPort(?int $port): UriInterface
@@ -391,7 +390,7 @@ public function getPart(string $key): ?string
391390
private function fillParts(): void
392391
{
393392
$vars = get_object_vars($this);
394-
foreach ($vars as $key => $valueNotUsed) {
393+
foreach ($vars as $key => $_valueNotUsed) {
395394
$this->encoded[$key] = null;
396395
$part = ($this->parts[$key] ?? null);
397396
if (!is_null($part)) {

0 commit comments

Comments
 (0)