Skip to content

Commit

Permalink
Declare strict types and enforce them, fixes #679
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerquix18 committed Oct 9, 2018
1 parent 2869707 commit 21ff80b
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 87 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php
dist: trusty
php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- nightly
Expand Down
31 changes: 16 additions & 15 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Abraham\TwitterOAuth;

Expand Down Expand Up @@ -43,10 +44,10 @@ class Config
* @param int $connectionTimeout
* @param int $timeout
*/
public function setTimeouts($connectionTimeout, $timeout)
public function setTimeouts(int $connectionTimeout, int $timeout): void
{
$this->connectionTimeout = (int)$connectionTimeout;
$this->timeout = (int)$timeout;
$this->connectionTimeout = $connectionTimeout;
$this->timeout = $timeout;
}

/**
Expand All @@ -55,32 +56,32 @@ public function setTimeouts($connectionTimeout, $timeout)
* @param int $maxRetries
* @param int $retriesDelay
*/
public function setRetries($maxRetries, $retriesDelay)
public function setRetries(int $maxRetries, int $retriesDelay): void
{
$this->maxRetries = (int)$maxRetries;
$this->retriesDelay = (int)$retriesDelay;
$this->maxRetries = $maxRetries;
$this->retriesDelay = $retriesDelay;
}

/**
* @param bool $value
*/
public function setDecodeJsonAsArray($value)
public function setDecodeJsonAsArray(bool $value): void
{
$this->decodeJsonAsArray = (bool)$value;
$this->decodeJsonAsArray = $value;
}

/**
* @param string $userAgent
*/
public function setUserAgent($userAgent)
public function setUserAgent(string $userAgent): void
{
$this->userAgent = (string)$userAgent;
$this->userAgent = $userAgent;
}

/**
* @param array $proxy
*/
public function setProxy(array $proxy)
public function setProxy(array $proxy): void
{
$this->proxy = $proxy;
}
Expand All @@ -90,18 +91,18 @@ public function setProxy(array $proxy)
*
* @param boolean $gzipEncoding
*/
public function setGzipEncoding($gzipEncoding)
public function setGzipEncoding(bool $gzipEncoding): void
{
$this->gzipEncoding = (bool)$gzipEncoding;
$this->gzipEncoding = $gzipEncoding;
}

/**
* Set the size of each part of file for chunked media upload.
*
* @param int $value
*/
public function setChunkSize($value)
public function setChunkSize(int $value): void
{
$this->chunkSize = (int)$value;
$this->chunkSize = $value;
}
}
8 changes: 5 additions & 3 deletions src/Consumer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* The MIT License
* Copyright (c) 2007 Andy Smith
Expand All @@ -15,11 +17,11 @@ class Consumer
public $callbackUrl;

/**
* @param string $key
* @param string $secret
* @param string|null $key
* @param string|null $secret
* @param null $callbackUrl
*/
public function __construct($key, $secret, $callbackUrl = null)
public function __construct(?string $key, ?string $secret, ?string $callbackUrl = null)
{
$this->key = $key;
$this->secret = $secret;
Expand Down
4 changes: 3 additions & 1 deletion src/HmacSha1.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* The MIT License
* Copyright (c) 2007 Andy Smith
Expand All @@ -25,7 +27,7 @@ public function getName()
/**
* {@inheritDoc}
*/
public function buildSignature(Request $request, Consumer $consumer, Token $token = null)
public function buildSignature(Request $request, Consumer $consumer, Token $token = null): string
{
$signatureBase = $request->getSignatureBaseString();

Expand Down
36 changes: 19 additions & 17 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* The MIT License
* Copyright (c) 2007 Andy Smith
Expand All @@ -20,7 +22,7 @@ class Request
* @param string $httpUrl
* @param array|null $parameters
*/
public function __construct($httpMethod, $httpUrl, array $parameters = [])
public function __construct(string $httpMethod, string $httpUrl, ?array $parameters = [])
{
$parameters = array_merge(Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)), $parameters);
$this->parameters = $parameters;
Expand All @@ -42,8 +44,8 @@ public function __construct($httpMethod, $httpUrl, array $parameters = [])
public static function fromConsumerAndToken(
Consumer $consumer,
Token $token = null,
$httpMethod,
$httpUrl,
string $httpMethod,
string $httpUrl,
array $parameters = [],
$json = false
) {
Expand Down Expand Up @@ -72,7 +74,7 @@ public static function fromConsumerAndToken(
* @param string $name
* @param string $value
*/
public function setParameter($name, $value)
public function setParameter(string $name, string $value)
{
$this->parameters[$name] = $value;
}
Expand All @@ -82,23 +84,23 @@ public function setParameter($name, $value)
*
* @return string|null
*/
public function getParameter($name)
public function getParameter(string $name): ?string
{
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
}

/**
* @return array
*/
public function getParameters()
public function getParameters(): array
{
return $this->parameters;
}

/**
* @param $name
*/
public function removeParameter($name)
public function removeParameter(string $name): void
{
unset($this->parameters[$name]);
}
Expand All @@ -108,7 +110,7 @@ public function removeParameter($name)
*
* @return string
*/
public function getSignableParameters()
public function getSignableParameters(): string
{
// Grab all parameters
$params = $this->parameters;
Expand All @@ -131,7 +133,7 @@ public function getSignableParameters()
*
* @return string
*/
public function getSignatureBaseString()
public function getSignatureBaseString(): string
{
$parts = [
$this->getNormalizedHttpMethod(),
Expand All @@ -149,7 +151,7 @@ public function getSignatureBaseString()
*
* @return string
*/
public function getNormalizedHttpMethod()
public function getNormalizedHttpMethod(): string
{
return strtoupper($this->httpMethod);
}
Expand All @@ -160,7 +162,7 @@ public function getNormalizedHttpMethod()
*
* @return string
*/
public function getNormalizedHttpUrl()
public function getNormalizedHttpUrl(): string
{
$parts = parse_url($this->httpUrl);

Expand All @@ -176,7 +178,7 @@ public function getNormalizedHttpUrl()
*
* @return string
*/
public function toUrl()
public function toUrl(): string
{
$postData = $this->toPostdata();
$out = $this->getNormalizedHttpUrl();
Expand All @@ -191,7 +193,7 @@ public function toUrl()
*
* @return string
*/
public function toPostdata()
public function toPostdata(): string
{
return Util::buildHttpQuery($this->parameters);
}
Expand All @@ -202,7 +204,7 @@ public function toPostdata()
* @return string
* @throws TwitterOAuthException
*/
public function toHeader()
public function toHeader(): string
{
$first = true;
$out = 'Authorization: OAuth';
Expand All @@ -223,7 +225,7 @@ public function toHeader()
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->toUrl();
}
Expand All @@ -247,15 +249,15 @@ public function signRequest(SignatureMethod $signatureMethod, Consumer $consumer
*
* @return string
*/
public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null)
public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null): string
{
return $signatureMethod->buildSignature($this, $consumer, $token);
}

/**
* @return string
*/
public static function generateNonce()
public static function generateNonce(): string
{
return md5(microtime() . mt_rand());
}
Expand Down
17 changes: 9 additions & 8 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Abraham\TwitterOAuth;

Expand All @@ -23,15 +24,15 @@ class Response
/**
* @param string $apiPath
*/
public function setApiPath($apiPath)
public function setApiPath(string $apiPath): void
{
$this->apiPath = $apiPath;
}

/**
* @return string|null
*/
public function getApiPath()
public function getApiPath(): ?string
{
return $this->apiPath;
}
Expand All @@ -55,23 +56,23 @@ public function getBody()
/**
* @param int $httpCode
*/
public function setHttpCode($httpCode)
public function setHttpCode(int $httpCode): void
{
$this->httpCode = $httpCode;
}

/**
* @return int
*/
public function getHttpCode()
public function getHttpCode(): int
{
return $this->httpCode;
}

/**
* @param array $headers
*/
public function setHeaders(array $headers)
public function setHeaders(array $headers): void
{
foreach ($headers as $key => $value) {
if (substr($key, 0, 1) == 'x') {
Expand All @@ -84,23 +85,23 @@ public function setHeaders(array $headers)
/**
* @return array
*/
public function getsHeaders()
public function getsHeaders(): array
{
return $this->headers;
}

/**
* @param array $xHeaders
*/
public function setXHeaders(array $xHeaders = [])
public function setXHeaders(array $xHeaders = []): void
{
$this->xHeaders = $xHeaders;
}

/**
* @return array
*/
public function getXHeaders()
public function getXHeaders(): array
{
return $this->xHeaders;
}
Expand Down
4 changes: 3 additions & 1 deletion src/SignatureMethod.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* The MIT License
* Copyright (c) 2007 Andy Smith
Expand Down Expand Up @@ -42,7 +44,7 @@ abstract public function buildSignature(Request $request, Consumer $consumer, To
*
* @return bool
*/
public function checkSignature(Request $request, Consumer $consumer, Token $token, $signature)
public function checkSignature(Request $request, Consumer $consumer, Token $token, string $signature): bool
{
$built = $this->buildSignature($request, $consumer, $token);

Expand Down
Loading

0 comments on commit 21ff80b

Please sign in to comment.