Fixed all possible PHPStan errors

This commit is contained in:
Alejandro Celaya
2017-12-27 16:23:54 +01:00
parent 4f3995ea80
commit db956a1f40
21 changed files with 67 additions and 68 deletions

View File

@@ -50,7 +50,7 @@ class CreateShortcodeAction extends AbstractRestAction
*/
public function process(Request $request, DelegateInterface $delegate)
{
$postData = $request->getParsedBody();
$postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) {
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,

View File

@@ -75,10 +75,10 @@ class GetVisitsAction extends AbstractRestAction
/**
* @param Request $request
* @param $key
* @param string $key
* @return \DateTime|null
*/
protected function getDateQueryParam(Request $request, $key)
private function getDateQueryParam(Request $request, string $key)
{
$query = $request->getQueryParams();
if (! isset($query[$key]) || empty($query[$key])) {

View File

@@ -27,7 +27,7 @@ class JWTService implements JWTServiceInterface
* @param int $lifetime
* @return string
*/
public function create(ApiKey $apiKey, $lifetime = self::DEFAULT_LIFETIME)
public function create(ApiKey $apiKey, $lifetime = self::DEFAULT_LIFETIME): string
{
$currentTimestamp = time();
@@ -48,7 +48,7 @@ class JWTService implements JWTServiceInterface
* @return string
* @throws AuthenticationException If the token has expired
*/
public function refresh($jwt, $lifetime = self::DEFAULT_LIFETIME)
public function refresh(string $jwt, $lifetime = self::DEFAULT_LIFETIME): string
{
$payload = $this->getPayload($jwt);
$payload['exp'] = time() + $lifetime;
@@ -61,7 +61,7 @@ class JWTService implements JWTServiceInterface
* @param string $jwt
* @return bool
*/
public function verify($jwt)
public function verify(string $jwt): bool
{
try {
// If no exception is thrown while decoding the token, it is considered valid
@@ -79,7 +79,7 @@ class JWTService implements JWTServiceInterface
* @return array
* @throws AuthenticationException If the token has expired
*/
public function getPayload($jwt)
public function getPayload(string $jwt): array
{
try {
return $this->decode($jwt);
@@ -92,16 +92,16 @@ class JWTService implements JWTServiceInterface
* @param array $data
* @return string
*/
protected function encode(array $data)
protected function encode(array $data): string
{
return JWT::encode($data, $this->appOptions->getSecretKey(), self::DEFAULT_ENCRYPTION_ALG);
}
/**
* @param $jwt
* @param string $jwt
* @return array
*/
protected function decode($jwt)
protected function decode(string $jwt): array
{
return (array) JWT::decode($jwt, $this->appOptions->getSecretKey(), [self::DEFAULT_ENCRYPTION_ALG]);
}

View File

@@ -18,7 +18,7 @@ interface JWTServiceInterface
* @param int $lifetime
* @return string
*/
public function create(ApiKey $apiKey, $lifetime = self::DEFAULT_LIFETIME);
public function create(ApiKey $apiKey, $lifetime = self::DEFAULT_LIFETIME): string;
/**
* Refreshes a token and returns it with the new expiration
@@ -28,7 +28,7 @@ interface JWTServiceInterface
* @return string
* @throws AuthenticationException If the token has expired
*/
public function refresh($jwt, $lifetime = self::DEFAULT_LIFETIME);
public function refresh(string $jwt, $lifetime = self::DEFAULT_LIFETIME): string;
/**
* Verifies that certain JWT is valid
@@ -36,7 +36,7 @@ interface JWTServiceInterface
* @param string $jwt
* @return bool
*/
public function verify($jwt);
public function verify(string $jwt): bool;
/**
* Decodes certain token and returns the payload
@@ -45,5 +45,5 @@ interface JWTServiceInterface
* @return array
* @throws AuthenticationException If the token has expired
*/
public function getPayload($jwt);
public function getPayload(string $jwt): array;
}

View File

@@ -55,13 +55,14 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
*
* @return Response
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(Request $request, DelegateInterface $delegate)
{
// If current route is the authenticate route or an OPTIONS request, continue to the next middleware
/** @var RouteResult $routeResult */
/** @var RouteResult|null $routeResult */
$routeResult = $request->getAttribute(RouteResult::class);
if (! isset($routeResult)
if ($routeResult === null
|| $routeResult->isFailure()
|| $routeResult->getMatchedRouteName() === AuthenticateAction::class
|| $request->getMethod() === 'OPTIONS'

View File

@@ -46,13 +46,9 @@ class ApiKeyService implements ApiKeyServiceInterface
*/
public function check($key)
{
/** @var ApiKey $apiKey */
/** @var ApiKey|null $apiKey */
$apiKey = $this->getByKey($key);
if (! isset($apiKey)) {
return false;
}
return $apiKey->isValid();
return $apiKey !== null && $apiKey->isValid();
}
/**
@@ -60,12 +56,13 @@ class ApiKeyService implements ApiKeyServiceInterface
*
* @param string $key
* @return ApiKey
* @throws InvalidArgumentException
*/
public function disable($key)
{
/** @var ApiKey $apiKey */
/** @var ApiKey|null $apiKey */
$apiKey = $this->getByKey($key);
if (! isset($apiKey)) {
if ($apiKey === null) {
throw new InvalidArgumentException(sprintf('API key "%s" does not exist and can\'t be disabled', $key));
}
@@ -75,7 +72,7 @@ class ApiKeyService implements ApiKeyServiceInterface
}
/**
* Lists all existing appi keys
* Lists all existing api keys
*
* @param bool $enabledOnly Tells if only enabled keys should be returned
* @return ApiKey[]
@@ -94,8 +91,10 @@ class ApiKeyService implements ApiKeyServiceInterface
*/
public function getByKey($key)
{
return $this->em->getRepository(ApiKey::class)->findOneBy([
/** @var ApiKey|null $apiKey */
$apiKey = $this->em->getRepository(ApiKey::class)->findOneBy([
'key' => $key,
]);
return $apiKey;
}
}

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Service;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ApiKeyServiceInterface
@@ -28,11 +29,12 @@ interface ApiKeyServiceInterface
*
* @param string $key
* @return ApiKey
* @throws InvalidArgumentException
*/
public function disable($key);
/**
* Lists all existing appi keys
* Lists all existing api keys
*
* @param bool $enabledOnly Tells if only enabled keys should be returned
* @return ApiKey[]