Skip to content

Commit

Permalink
Add more scalar type checks and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Jan 26, 2019
1 parent a4da600 commit 1dab787
Show file tree
Hide file tree
Showing 51 changed files with 374 additions and 567 deletions.
15 changes: 15 additions & 0 deletions .phpstorm.meta.php/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@
\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::get(0),
map([])
);

override(
\TYPO3\CMS\Extbase\Object\ObjectManager::get(0),
map([])
);

override(
\Cundd\Rest\ObjectManagerInterface::get(0),
map([])
);

override(
\Cundd\Rest\ObjectManager::get(0),
map([])
);
}
2 changes: 1 addition & 1 deletion Classes/Access/AbstractAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(ObjectManager $objectManager)
* @return Access
* @throws \Exception
*/
protected function checkAuthentication(RestRequestInterface $request)
protected function checkAuthentication(RestRequestInterface $request): Access
{
try {
$isAuthenticated = $this->objectManager->getAuthenticationProvider()->authenticate($request);
Expand Down
5 changes: 2 additions & 3 deletions Classes/Access/AccessControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ interface AccessControllerInterface
* @param RestRequestInterface $request
* @return Access
*/
public function getAccess(RestRequestInterface $request);
public function getAccess(RestRequestInterface $request): Access;

/**
* Returns if the given request needs authentication
*
* @param RestRequestInterface $request
* @return bool
*/
public function requestNeedsAuthentication(RestRequestInterface $request);

public function requestNeedsAuthentication(RestRequestInterface $request): bool;
}
8 changes: 4 additions & 4 deletions Classes/Access/ConfigurationBasedAccessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(ConfigurationProviderInterface $configurationProvide
* @return Access
* @throws \Exception
*/
public function getAccess(RestRequestInterface $request)
public function getAccess(RestRequestInterface $request): Access
{
$access = $this->getAccessConfiguration($request);
if ($access->isRequireLogin()) {
Expand Down Expand Up @@ -70,7 +70,7 @@ public function getConfigurationForResourceType(ResourceType $resourceType)
* @return bool
* @throws Exception\InvalidConfigurationException
*/
public function requestNeedsAuthentication(RestRequestInterface $request)
public function requestNeedsAuthentication(RestRequestInterface $request): bool
{
return $this->getAccessConfiguration($request)->isRequireLogin();
}
Expand All @@ -81,7 +81,7 @@ public function requestNeedsAuthentication(RestRequestInterface $request)
* @param RestRequestInterface $request
* @return bool
*/
protected function requiresAuthorization($request)
protected function requiresAuthorization($request): bool
{
return !in_array(strtoupper($request->getMethod()), self::ACCESS_NOT_REQUIRED);
}
Expand All @@ -90,7 +90,7 @@ protected function requiresAuthorization($request)
* @param RestRequestInterface $request
* @return Access
*/
protected function getAccessConfiguration(RestRequestInterface $request)
protected function getAccessConfiguration(RestRequestInterface $request): Access
{
if (!$this->requiresAuthorization($request)) {
return Access::allowed();
Expand Down
2 changes: 1 addition & 1 deletion Classes/Authentication/AbstractAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class AbstractAuthenticationProvider implements AuthenticationProviderI
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request)
public function authenticate(RestRequestInterface $request): bool
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct($providers)
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request)
public function authenticate(RestRequestInterface $request): bool
{
/** @var AuthenticationProviderInterface $authenticationProvider */
foreach ($this->providers as $authenticationProvider) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Authentication/AuthenticationProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface AuthenticationProviderInterface
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request);
public function authenticate(RestRequestInterface $request): bool;
}
6 changes: 5 additions & 1 deletion Classes/Authentication/BasicAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BasicAuthenticationProvider extends AbstractAuthenticationProvider
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request)
public function authenticate(RestRequestInterface $request): bool
{
$username = null;
$password = null;
Expand All @@ -40,6 +40,10 @@ public function authenticate(RestRequestInterface $request)
list($username, $password) = $tuple;
}

if (!is_string($username) || !is_string($password)) {
return false;
}

return $this->userProvider->checkCredentials($username, $password);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CredentialsAuthenticationProvider extends AbstractAuthenticationProvider
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request)
public function authenticate(RestRequestInterface $request): bool
{
return $this->sessionManager->valueForKey('loginStatus') === AuthHandler::STATUS_LOGGED_IN;
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Authentication/RequestAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RequestAuthenticationProvider extends AbstractAuthenticationProvider
* @param RestRequestInterface $request
* @return bool Returns if the authentication was successful
*/
public function authenticate(RestRequestInterface $request)
public function authenticate(RestRequestInterface $request): bool
{
return !!($GLOBALS['TSFE']->fe_user->user);
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Authentication/UserProvider/FeUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class FeUserProvider implements UserProviderInterface
* @param string $password
* @return boolean
*/
public function checkCredentials($username, $password)
public function checkCredentials(string $username, string $password): bool
{
if (!is_string($password) || '' === $password) {
if ('' === $username || '' === $password) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Authentication/UserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface UserProviderInterface
* @param string $password
* @return boolean
*/
public function checkCredentials($username, $password);
public function checkCredentials(string $username, string $password): bool;
}
16 changes: 8 additions & 8 deletions Classes/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class Cache implements CacheInterface
*
* @var integer
*/
private $cacheLifeTime;
private $cacheLifeTime = 0;

/**
* Life time defined in the expires header
*
* @var integer
*/
private $expiresHeaderLifeTime;
private $expiresHeaderLifeTime = 0;

/**
* @var ResponseFactory
Expand All @@ -54,7 +54,7 @@ public function __construct(ResponseFactoryInterface $responseFactory)
$this->responseFactory = $responseFactory;
}

public function getCachedValueForRequest(RestRequestInterface $request)
public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface
{
$cacheLifeTime = $this->getCacheLifeTime();

Expand Down Expand Up @@ -133,7 +133,7 @@ public function setCachedValueForRequest(
* @param RestRequestInterface $request
* @return string
*/
public function getCacheKeyForRequest(RestRequestInterface $request)
public function getCacheKeyForRequest(RestRequestInterface $request): string
{
$cacheKey = sha1($request->getUri() . '_' . $request->getFormat() . '_' . $request->getMethod());
$params = $request->getQueryParams();
Expand All @@ -150,7 +150,7 @@ public function getCacheKeyForRequest(RestRequestInterface $request)
* @param int $cacheLifeTime
* @return $this
*/
public function setCacheLifeTime($cacheLifeTime)
public function setCacheLifeTime(int $cacheLifeTime): CacheInterface
{
$this->cacheLifeTime = $cacheLifeTime;

Expand All @@ -162,7 +162,7 @@ public function setCacheLifeTime($cacheLifeTime)
*
* @return int
*/
public function getCacheLifeTime()
public function getCacheLifeTime(): int
{
return $this->cacheLifeTime;
}
Expand All @@ -173,7 +173,7 @@ public function getCacheLifeTime()
* @param int $expiresHeaderLifeTime
* @return $this
*/
public function setExpiresHeaderLifeTime($expiresHeaderLifeTime)
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): CacheInterface
{
$this->expiresHeaderLifeTime = $expiresHeaderLifeTime;

Expand All @@ -185,7 +185,7 @@ public function setExpiresHeaderLifeTime($expiresHeaderLifeTime)
*
* @return int
*/
public function getExpiresHeaderLifeTime()
public function getExpiresHeaderLifeTime(): int
{
return $this->expiresHeaderLifeTime;
}
Expand Down
13 changes: 7 additions & 6 deletions Classes/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class CacheFactory
* @param ObjectManager $objectManager
* @return CacheInterface
*/
public function buildCache(ConfigurationProviderInterface $configurationProvider, ObjectManager $objectManager)
{
public function buildCache(
ConfigurationProviderInterface $configurationProvider,
ObjectManager $objectManager
): CacheInterface {
$cacheInstance = $this->getCacheInstance($configurationProvider, $objectManager);

if ($cacheInstance instanceof CacheInterface) {
Expand Down Expand Up @@ -46,22 +48,21 @@ private function getCacheInstance(
* @param ConfigurationProviderInterface $configurationProvider
* @return int|mixed
*/
private function getCacheLifeTime(ConfigurationProviderInterface $configurationProvider)
private function getCacheLifeTime(ConfigurationProviderInterface $configurationProvider): int
{
$readCacheLifeTime = $configurationProvider->getSetting('cacheLifeTime');
if ($readCacheLifeTime === null) {
$readCacheLifeTime = -1;
}
$readCacheLifeTime = intval($readCacheLifeTime);

return $readCacheLifeTime;
return (int)$readCacheLifeTime;
}

/**
* @param ConfigurationProviderInterface $configurationProvider
* @return int|mixed
*/
private function getExpiresHeaderLifeTime(ConfigurationProviderInterface $configurationProvider)
private function getExpiresHeaderLifeTime(ConfigurationProviderInterface $configurationProvider): int
{
$expiresHeaderLifeTime = $configurationProvider->getSetting('expiresHeaderLifeTime');

Expand Down
12 changes: 6 additions & 6 deletions Classes/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface CacheInterface
* @param RestRequestInterface $request
* @return ResponseInterface|null
*/
public function getCachedValueForRequest(RestRequestInterface $request);
public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface;

/**
* Sets the cache value for the given request
Expand All @@ -41,35 +41,35 @@ public function setCachedValueForRequest(
* @param RestRequestInterface $request
* @return string
*/
public function getCacheKeyForRequest(RestRequestInterface $request);
public function getCacheKeyForRequest(RestRequestInterface $request): string;

/**
* Sets the cache life time
*
* @param int $cacheLifeTime
* @return $this
*/
public function setCacheLifeTime($cacheLifeTime);
public function setCacheLifeTime(int $cacheLifeTime): self;

/**
* Returns the cache life time
*
* @return int
*/
public function getCacheLifeTime();
public function getCacheLifeTime(): int;

/**
* Sets the life time defined in the expires header
*
* @param int $expiresHeaderLifeTime
* @return $this
*/
public function setExpiresHeaderLifeTime($expiresHeaderLifeTime);
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): self;

/**
* Returns the life time defined in the expires header
*
* @return int
*/
public function getExpiresHeaderLifeTime();
public function getExpiresHeaderLifeTime(): int;
}
8 changes: 4 additions & 4 deletions Classes/Configuration/AbstractConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractConfigurationProvider implements SingletonInterface, Conf
* @param mixed $defaultValue
* @return mixed
*/
public function getSetting($keyPath, $defaultValue = null)
public function getSetting(string $keyPath, $defaultValue = null)
{
$matchingSetting = $this->getSettings();

Expand Down Expand Up @@ -58,7 +58,7 @@ public function getSetting($keyPath, $defaultValue = null)
*
* @return array
*/
public function getSettings()
public function getSettings(): array
{
if ($this->settings === null) {
throw new InvalidConfigurationException('No settings provided');
Expand All @@ -84,7 +84,7 @@ public function setSettings($settings)
* @param ResourceType $resourceType
* @return ResourceConfiguration
*/
public function getResourceConfiguration(ResourceType $resourceType)
public function getResourceConfiguration(ResourceType $resourceType): ?ResourceConfiguration
{
$configuredPaths = $this->getConfiguredResources();
$matchingConfiguration = null;
Expand Down Expand Up @@ -122,7 +122,7 @@ public function getResourceConfiguration(ResourceType $resourceType)
*
* @return ResourceConfiguration[]
*/
public function getConfiguredResources()
public function getConfiguredResources(): array
{
$configurationCollection = [];
foreach ($this->getRawConfiguredResourceTypes() as $path => $configuration) {
Expand Down
8 changes: 4 additions & 4 deletions Classes/Configuration/ConfigurationProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ interface ConfigurationProviderInterface
* @param mixed $defaultValue
* @return mixed
*/
public function getSetting($keyPath, $defaultValue = null);
public function getSetting(string $keyPath, $defaultValue = null);

/**
* Returns the settings read from the TypoScript
*
* @return array
*/
public function getSettings();
public function getSettings(): array;

/**
* Returns the paths configured in the settings
*
* @return ResourceConfiguration[]
*/
public function getConfiguredResources();
public function getConfiguredResources(): array;

/**
* Returns the configuration matching the given resource type
*
* @param ResourceType $resourceType
* @return ResourceConfiguration|null
*/
public function getResourceConfiguration(ResourceType $resourceType);
public function getResourceConfiguration(ResourceType $resourceType): ?\Cundd\Rest\Configuration\ResourceConfiguration;
}
Loading

0 comments on commit 1dab787

Please sign in to comment.