Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
fixes to follow coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
fain182 committed Jan 10, 2019
1 parent b5fe6d8 commit acf8e1f
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 100 deletions.
1 change: 1 addition & 0 deletions .php_cs.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"7.2.13","version":"2.14.0","rules":{"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"braces":{"allow_single_line_closure":true},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"class_definition":{"single_line":true},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"method_argument_space":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"protected_to_private":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_class_element_per_statement":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true,"blank_line_after_namespace":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"tests\/Unit\/RequestTest.php":687563615,"tests\/Functional\/HttpClientTest.php":3123148743,"tests\/Functional\/HttpClientTimeoutTest.php":2450938912,"tests\/Functional\/HttpClientErrorTest.php":4282566682,"tests\/Functional\/HttpClientSslTest.php":3214603577,"src\/HttpClient.php":2990201965,"src\/CurlHandleFactory.php":1830172169,"src\/JsonRequest.php":3128360305,"src\/Request.php":3744346561,"src\/NetworkException.php":294924631,"src\/RequestException.php":1428655463}}
14 changes: 14 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('bin')
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true
])
->setFinder($finder)
;
14 changes: 6 additions & 8 deletions src/CurlHandleFactory.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php


namespace Diciotto;


use Psr\Http\Message\RequestInterface;

class CurlHandleFactory
{

public static function build(RequestInterface $request) {
public static function build(RequestInterface $request)
{
$curl = curl_init();

curl_setopt($curl,CURLOPT_URL, $request->getUri());
curl_setopt($curl, CURLOPT_URL, $request->getUri());
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

Expand All @@ -25,7 +23,7 @@ public static function build(RequestInterface $request) {
break;
case 'PUT':
case 'DELETE':
curl_setopt($curl, CURLOPT_POSTFIELDS, (string)$request->getBody());
curl_setopt($curl, CURLOPT_POSTFIELDS, (string) $request->getBody());
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod());
break;
default:
Expand All @@ -34,11 +32,11 @@ public static function build(RequestInterface $request) {

$headerLines = [];
foreach ($request->getHeaders() as $headerName => $values) {
$headerLines[] = $headerName.": ".$request->getHeaderLine($headerName);
$headerLines[] = $headerName.': '.$request->getHeaderLine($headerName);
}

curl_setopt($curl, CURLOPT_HTTPHEADER, $headerLines);

return $curl;
}
}
}
36 changes: 21 additions & 15 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ class HttpClient implements ClientInterface
private $timeoutInSeconds = 15;
private $checkSslCertificate = true;

public function withTimeout(int $timeoutInSeconds) : self
public function withTimeout(int $timeoutInSeconds): self
{
$this->timeoutInSeconds = $timeoutInSeconds;

return $this;
}

public function withCheckSslCertificates(bool $checkSslCertificate) : self
public function withCheckSslCertificates(bool $checkSslCertificate): self
{
$this->checkSslCertificate = $checkSslCertificate;

return $this;
}

Expand All @@ -31,33 +33,33 @@ public function withCheckSslCertificates(bool $checkSslCertificate) : self
*
* @return ResponseInterface
*
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
* @throws \Psr\Http\Client\ClientExceptionInterface if an error happens while processing the request
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
$curl = CurlHandleFactory::build($request);

curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeoutInSeconds);

if ($this->checkSslCertificate === false) {
if (false === $this->checkSslCertificate) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}


$headerLines = [];
curl_setopt(
$curl,
CURLOPT_HEADERFUNCTION,
function ($curl, $headerLine) use (&$headerLines) {
$len = strlen($headerLine);
$headerLines []= $headerLine;
$headerLines[] = $headerLine;

return $len;
}
);

$responseBody = curl_exec($curl);
if ($responseBody === false) {
if (false === $responseBody) {
throw new NetworkException(curl_error($curl), $request);
}
curl_close($curl);
Expand All @@ -70,17 +72,22 @@ function ($curl, $headerLine) use (&$headerLines) {
return new Response($statusCode, $headers, $responseBody, $version);
}

private function parseHttpVersion($headerLines): string {
private function parseHttpVersion($headerLines): string
{
preg_match('/http\/(.+) (\d+) /i', $headerLines[0], $matches);

return $matches[1];
}

private function parseStatusCode($headerLines): int {
private function parseStatusCode($headerLines): int
{
preg_match('/http\/(.+) (\d+) /i', $headerLines[0], $matches);

return $matches[2];
}

private function parseHttpHeaders($headerLines): array {
private function parseHttpHeaders($headerLines): array
{
array_shift($headerLines);

$headers = [];
Expand All @@ -98,16 +105,15 @@ private function parseHttpHeaders($headerLines): array {
* When curl follow redirects give us the headers of all the request instead of only the last one,
* so we need to select the headers only for the last request.
*/
private function discardRedirectsHeaders($headerLines): array {
private function discardRedirectsHeaders($headerLines): array
{
$lastHttpRequestStartAtIndex = 0;
for ($i = 0; $i < count($headerLines); $i++) {
for ($i = 0; $i < count($headerLines); ++$i) {
if (preg_match('/http\/(.+) (\d+) /i', $headerLines[$i], $matches)) {
$lastHttpRequestStartAtIndex = $i;
}
}

return array_slice($headerLines, $lastHttpRequestStartAtIndex);
}


}
}
5 changes: 1 addition & 4 deletions src/JsonRequest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php


namespace Diciotto;


class JsonRequest extends Request
{

public function __construct(string $uri, string $method = 'GET', array $body = null)
{
parent::__construct($uri, $method, json_encode($body));
$this->psrRequest = $this->psrRequest->withHeader('Content-Type', 'application/json');
$this->psrRequest = $this->psrRequest->withHeader('Accept', 'application/json');
}
}
}
4 changes: 1 addition & 3 deletions src/NetworkException.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php


namespace Diciotto;

use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Throwable;

class NetworkException extends \RuntimeException implements NetworkExceptionInterface
{
Expand All @@ -28,4 +26,4 @@ public function getRequest(): RequestInterface
{
return $this->request;
}
}
}
51 changes: 30 additions & 21 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,82 +10,88 @@ class Request implements RequestInterface
{
protected $psrRequest;

public function __construct($uri, $method = 'GET', $body = null) {
public function __construct($uri, $method = 'GET', $body = null)
{
$this->psrRequest = new \Nyholm\Psr7\Request($method, $uri, [], $body);
}

public function withCookie(string $name, string $value) : self
public function withCookie(string $name, string $value): self
{
return $this->withHeader("Cookie", "$name=$value");
return $this->withHeader('Cookie', "$name=$value");
}

public function withAddedCookie(string $name, string $value) : self
public function withAddedCookie(string $name, string $value): self
{
return $this->withAddedHeader("Cookie", "$name=$value");
return $this->withAddedHeader('Cookie', "$name=$value");
}

public function getHeaderLine($header) : string
public function getHeaderLine($header): string
{
$separator = ',';
if (strtolower($header) === 'cookie') {
if ('cookie' === strtolower($header)) {
$separator = ';';
}

return \implode($separator.' ', $this->getHeader($header));
}

/*
* From here there are only proxy methods for \Nyholm\Psr7\Request
*/

public function getProtocolVersion() : string
public function getProtocolVersion(): string
{
return $this->psrRequest->getProtocolVersion();
}

public function withProtocolVersion($version) : self
public function withProtocolVersion($version): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withProtocolVersion($version);

return $new;
}

public function getHeaders() : array
public function getHeaders(): array
{
return $this->psrRequest->getHeaders();
}

public function hasHeader($name) : bool
public function hasHeader($name): bool
{
return $this->psrRequest->hasHeader($name);
}

public function getHeader($name) : array
public function getHeader($name): array
{
return $this->psrRequest->getHeader($name);
}

public function withHeader($name, $value) : self
public function withHeader($name, $value): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withHeader($name, $value);

return $new;
}

public function withAddedHeader($name, $value) : self
public function withAddedHeader($name, $value): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withAddedHeader($name, $value);

return $new;
}

public function withoutHeader($name): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withoutHeader($name);

return $new;
}

public function getBody() : StreamInterface
public function getBody(): StreamInterface
{
return $this->psrRequest->getBody();
}
Expand All @@ -94,10 +100,11 @@ public function withBody(StreamInterface $body): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withBody($body);

return $new;
}

public function getRequestTarget() : string
public function getRequestTarget(): string
{
return $this->psrRequest->getRequestTarget();
}
Expand All @@ -106,22 +113,24 @@ public function withRequestTarget($requestTarget): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withRequestTarget($requestTarget);

return $new;
}

public function getMethod() : string
public function getMethod(): string
{
return $this->psrRequest->getMethod();
}

public function withMethod($method) : self
public function withMethod($method): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withMethod($method);

return $new;
}

public function getUri() : UriInterface
public function getUri(): UriInterface
{
return $this->psrRequest->getUri();
}
Expand All @@ -130,7 +139,7 @@ public function withUri(UriInterface $uri, $preserveHost = false): self
{
$new = clone $this;
$new->psrRequest = $this->psrRequest->withUri($uri, $preserveHost);

return $new;
}

}
}
6 changes: 1 addition & 5 deletions src/RequestException.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php


namespace Diciotto;


use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\RequestInterface;

class RequestException extends \RuntimeException implements RequestExceptionInterface
{

private $request;

public function __construct(string $message, RequestInterface $request)
Expand All @@ -29,5 +26,4 @@ public function getRequest(): RequestInterface
{
return $this->request;
}

}
}
Loading

0 comments on commit acf8e1f

Please sign in to comment.