Skip to content

Commit

Permalink
Fixes deprecation warning (#17)
Browse files Browse the repository at this point in the history
Also drops support for PHP 5.x
  • Loading branch information
DamienHarper authored and mariusbalcytis committed Nov 14, 2019
1 parent dbfa7f4 commit 7cc8f5c
Show file tree
Hide file tree
Showing 26 changed files with 40 additions and 96 deletions.
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ env:

matrix:
include:
- php: 5.5
env: COMPOSER_ARGS=""
- php: 5.6
env: COMPOSER_ARGS=""
- php: 7.0
env: COMPOSER_ARGS=""
- php: 7.1
Expand All @@ -25,10 +21,6 @@ matrix:
- php: 7.3
env: COMPOSER_ARGS="" WITH_CS="true"

- php: 5.5
env: COMPOSER_ARGS="--prefer-lowest"
- php: 5.6
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.0
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.1
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["symfony bundle", "rate limiter", "rate limiting", "rate limit", "brute force protection", "leaky bucket", "token bucket", "threshold", "throttle", "security", "dos protection", "brute-force", "bruteforce"],
"type": "symfony-bundle",
"require": {
"php": "^5.5|^7.0",
"php": "^7.0",
"maba/gentle-force": "^0.2.1|^0.3",
"symfony/framework-bundle": "^2.7.50|^3.0|^4.0",
"maba/dependency-injection-extra": "^0.1.1|^1.0",
Expand Down
2 changes: 0 additions & 2 deletions src/Command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param ListenerConfiguration[] $configurationList
* @return ListenerConfiguration
*/
Expand Down
17 changes: 8 additions & 9 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('maba_gentle_force');
$treeBuilder = new TreeBuilder('maba_gentle_force');
// Keep compatibility with symfony/config < 4.2
$rootNode = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('maba_gentle_force');

$children = $rootNode->children();

Expand Down Expand Up @@ -51,14 +52,14 @@ private function configureRedis(ArrayNodeDefinition $node)
$node->validate()->ifTrue(function ($nodeConfig) {
if (isset($nodeConfig['host'])) {
return isset($nodeConfig['service_id'])
|| (isset($nodeConfig['parameters']) && count($nodeConfig['parameters']) > 0)
|| (isset($nodeConfig['parameters']) && \count($nodeConfig['parameters']) > 0)
|| isset($nodeConfig['options'])
;
}

if (isset($nodeConfig['service_id'])) {
return isset($nodeConfig['host'])
|| (isset($nodeConfig['parameters']) && count($nodeConfig['parameters']) > 0)
|| (isset($nodeConfig['parameters']) && \count($nodeConfig['parameters']) > 0)
|| isset($nodeConfig['options'])
;
}
Expand Down Expand Up @@ -160,9 +161,7 @@ private function buildStatusesNode(NodeBuilder $node, $name)
'max_range' => 599,
]]);
if ($validatedStatusCode === false) {
throw new InvalidConfigurationException(
sprintf('Status code %s is invalid', $statusCode)
);
throw new InvalidConfigurationException(sprintf('Status code %s is invalid', $statusCode));
}

return $validatedStatusCode;
Expand All @@ -177,10 +176,10 @@ private function addSuccessMatcherValidation(ArrayNodeDefinition $node)
if (isset($configuration['success_matcher'])) {
$count++;
}
if (count($configuration['success_statuses']) > 0) {
if (\count($configuration['success_statuses']) > 0) {
$count++;
}
if (count($configuration['failure_statuses']) > 0) {
if (\count($configuration['failure_statuses']) > 0) {
$count++;
}

Expand Down
33 changes: 13 additions & 20 deletions src/DependencyInjection/MabaGentleForceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Maba\GentleForce\RateLimitProvider;
use Predis\Client;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\KernelEvents;

class MabaGentleForceExtension extends Extension
Expand Down Expand Up @@ -96,7 +96,7 @@ private function registerRedisClient(ContainerBuilder $container, XmlFileLoader

if (isset($redisConfig['host'])) {
$parameters = ['host' => $redisConfig['host']];
} elseif (isset($redisConfig['parameters']) && count($redisConfig['parameters']) > 0) {
} elseif (isset($redisConfig['parameters']) && \count($redisConfig['parameters']) > 0) {
$parameters = $redisConfig['parameters'];
$options = $redisConfig['options'];
}
Expand All @@ -114,7 +114,7 @@ private function registerListeners(
$strategiesConfiguration = $config['strategies'];
$availableLimitsKeys = array_keys($config['limits']);

if (count($listenerConfigList) === 0) {
if (\count($listenerConfigList) === 0) {
return;
}

Expand All @@ -136,11 +136,8 @@ private function registerListeners(
$usedStrategies[] = $strategyId;

$limitsKey = $listenerConfig['limits_key'];
if (!in_array($limitsKey, $availableLimitsKeys, true)) {
throw new InvalidConfigurationException(sprintf(
'Specified limits_key (%s) is not registered',
$limitsKey
));
if (!\in_array($limitsKey, $availableLimitsKeys, true)) {
throw new InvalidConfigurationException(sprintf('Specified limits_key (%s) is not registered', $limitsKey));
}

$pathPattern = '#' . str_replace('#', '\\#', $listenerConfig['path']) . '#';
Expand Down Expand Up @@ -170,14 +167,14 @@ private function buildSuccessMatcher(array $listenerConfig)
return new Reference($listenerConfig['success_matcher']);
}

if (count($listenerConfig['success_statuses']) > 0) {
if (\count($listenerConfig['success_statuses']) > 0) {
return new Definition(
ResponseCodeSuccessMatcher::class,
[$listenerConfig['success_statuses']]
);
}

if (count($listenerConfig['failure_statuses']) > 0) {
if (\count($listenerConfig['failure_statuses']) > 0) {
return new Definition(
ResponseCodeSuccessMatcher::class,
[$listenerConfig['failure_statuses'], true]
Expand Down Expand Up @@ -205,30 +202,26 @@ private function includeStrategyDefinitions(
array $usedStrategies,
array $config
) {
if (in_array('maba_gentle_force.strategy.log', $usedStrategies, true)) {
if (\in_array('maba_gentle_force.strategy.log', $usedStrategies, true)) {
$loader->load('log_strategy.xml');
}

$recaptchaNeeded = false;
if (in_array('maba_gentle_force.strategy.recaptcha_headers', $usedStrategies, true)) {
if (\in_array('maba_gentle_force.strategy.recaptcha_headers', $usedStrategies, true)) {
$loader->load('recaptcha/headers.xml');
$recaptchaNeeded = true;
}
if (in_array('maba_gentle_force.strategy.recaptcha_template', $usedStrategies, true)) {
if (\in_array('maba_gentle_force.strategy.recaptcha_template', $usedStrategies, true)) {
$loader->load('recaptcha/template.xml');
$recaptchaNeeded = true;
}
if ($recaptchaNeeded) {
if (!isset($config['recaptcha'])) {
throw new InvalidConfigurationException(
'You need to configure "recaptcha" node to use any of recaptcha_* strategies'
);
throw new InvalidConfigurationException('You need to configure "recaptcha" node to use any of recaptcha_* strategies');
}

if (!class_exists('ReCaptcha\ReCaptcha')) {
throw new InvalidConfigurationException(
'You need to install "google/recaptcha" library to use any of recaptcha_* strategies'
);
throw new InvalidConfigurationException('You need to install "google/recaptcha" library to use any of recaptcha_* strategies');
}

$loader->load('recaptcha/main.xml');
Expand Down
10 changes: 2 additions & 8 deletions src/DependencyInjection/StrategyValidationCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ public function process(ContainerBuilder $container)

foreach ($strategyIdList as $strategyId) {
if (!$container->hasDefinition($strategyId)) {
throw new InvalidConfigurationException(sprintf(
'Gentle force strategy "%s" does not exist in service container',
$strategyId
));
throw new InvalidConfigurationException(sprintf('Gentle force strategy "%s" does not exist in service container', $strategyId));
}

$definition = $container->getDefinition($strategyId);
if (!$definition->isPublic()) {
throw new InvalidConfigurationException(sprintf(
'Service "%s" must be public as this is required for gentle force strategies',
$strategyId
));
throw new InvalidConfigurationException(sprintf('Service "%s" must be public as this is required for gentle force strategies', $strategyId));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Listener/CompositeIncreaseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function getConfigurations()
}

/**
* @param ListenerConfiguration $configuration
* @return IncreaseResult
*/
public function getResultByConfiguration(ListenerConfiguration $configuration)
Expand Down
4 changes: 2 additions & 2 deletions src/Listener/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function isWhitelisted($request)
{
$controller = $request->attributes->get('_controller');

return $controller !== null && in_array($controller, $this->whitelistedControllers, true);
return $controller !== null && \in_array($controller, $this->whitelistedControllers, true);
}

private function checkAndIncrease(
Expand Down Expand Up @@ -99,7 +99,7 @@ public function resetForStrategies(Request $request, array $strategyList)
$identifierHelper = $this->createIdentifierHelper($request);

foreach ($this->configurationRegistry->getConfigurationList() as $configuration) {
if (in_array($configuration->getStrategyId(), $strategyList, true)) {
if (\in_array($configuration->getStrategyId(), $strategyList, true)) {
$this->reset($identifierHelper, $configuration);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Listener/IdentifierHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getIdentifier(ListenerConfiguration $configuration)

private function getConcreteIdentifierByType($identifierType)
{
if (array_key_exists($identifierType, $this->identifiers)) {
if (\array_key_exists($identifierType, $this->identifiers)) {
return $this->identifiers[$identifierType];
}

Expand Down
4 changes: 0 additions & 4 deletions src/Listener/ListenerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function getHosts()
}

/**
* @param array $hosts
* @return $this
*/
public function setHosts(array $hosts)
Expand All @@ -88,7 +87,6 @@ public function getMethods()
}

/**
* @param array $methods
* @return $this
*/
public function setMethods(array $methods)
Expand Down Expand Up @@ -126,7 +124,6 @@ public function getIdentifierTypes()
}

/**
* @param array $identifierTypes
* @return $this
*/
public function setIdentifierTypes(array $identifierTypes)
Expand Down Expand Up @@ -164,7 +161,6 @@ public function getSuccessMatcher()
}

/**
* @param SuccessMatcherInterface|null $successMatcher
* @return $this
*/
public function setSuccessMatcher(SuccessMatcherInterface $successMatcher = null)
Expand Down
1 change: 0 additions & 1 deletion src/Listener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public function onRequestFinished(FinishRequestEvent $event)
}

/**
* @param Request $request
* @return CompositeIncreaseResult|null
*/
private function getAndRemoveCompositeResult(Request $request)
Expand Down
8 changes: 4 additions & 4 deletions src/Listener/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function matches(ListenerConfiguration $configuration, Request $request)
return false;
}

if (count($configuration->getMethods()) > 0 && !$this->methodMatches($configuration, $request)) {
if (\count($configuration->getMethods()) > 0 && !$this->methodMatches($configuration, $request)) {
return false;
}

if (count($configuration->getHosts()) > 0 && !$this->hostMatches($configuration, $request)) {
if (\count($configuration->getHosts()) > 0 && !$this->hostMatches($configuration, $request)) {
return false;
}

Expand All @@ -30,11 +30,11 @@ private function pathMatches(ListenerConfiguration $configuration, Request $requ

private function methodMatches(ListenerConfiguration $configuration, Request $request)
{
return in_array($request->getMethod(), $configuration->getMethods(), true);
return \in_array($request->getMethod(), $configuration->getMethods(), true);
}

private function hostMatches(ListenerConfiguration $configuration, Request $request)
{
return in_array(strtolower($request->getHost()), $configuration->getHosts(), true);
return \in_array(strtolower($request->getHost()), $configuration->getHosts(), true);
}
}
1 change: 0 additions & 1 deletion src/Service/IdentifierBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class IdentifierBuilder
{
/**
* @param array $identifiers
* @return string
*
* @api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
interface IdentifierProviderInterface
{
/**
* @param Request $request
*
* @return string|null
*
* @api
Expand Down
8 changes: 2 additions & 6 deletions src/Service/RequestIdentifierProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function registerIdentifierProvider(IdentifierProviderInterface $identifi

/**
* @param string $identifierType
* @param Request $request
* @return null|string
* @return string|null
*
* @api
*/
Expand All @@ -46,10 +45,7 @@ public function getIdentifierPriority($identifierType)
private function getIdentifierProvider($identifierType)
{
if (!isset($this->identifierProviders[$identifierType])) {
throw new InvalidArgumentException(sprintf(
'Identifier provider for "%s" is not available',
$identifierType
));
throw new InvalidArgumentException(sprintf('Identifier provider for "%s" is not available', $identifierType));
}

return $this->identifierProviders[$identifierType];
Expand Down
3 changes: 0 additions & 3 deletions src/Service/ResponseModifyingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
interface ResponseModifyingStrategyInterface extends StrategyInterface
{
/**
* @param IncreaseResult $increaseResult
* @param Response $response
*
* @api
*/
public function modifyResponse(IncreaseResult $increaseResult, Response $response);
Expand Down
1 change: 0 additions & 1 deletion src/Service/Strategy/LogStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class LogStrategy implements StrategyInterface
private $level;

/**
* @param LoggerInterface $logger
* @param string $level
*/
public function __construct(LoggerInterface $logger, $level)
Expand Down
2 changes: 0 additions & 2 deletions src/Service/Strategy/RecaptchaHeadersStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class RecaptchaHeadersStrategy implements ResponseModifyingStrategyInterface
private $unlockUrlHeaderName;

/**
* @param ResponseModifyingStrategyInterface $internalStrategy
* @param string $headerName
* @param string $siteKey
* @param UrlGeneratorInterface $urlGenerator
* @param string $unlockUrlHeaderName
*/
public function __construct(
Expand Down
3 changes: 0 additions & 3 deletions src/Service/Strategy/RecaptchaTemplateStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class RecaptchaTemplateStrategy implements StrategyInterface
private $template;

/**
* @param EngineInterface $templating
* @param RequestStack $requestStack
* @param UrlGeneratorInterface $urlGenerator
* @param string $googleRecaptchaSiteKey
* @param string $template
*/
Expand Down
Loading

0 comments on commit 7cc8f5c

Please sign in to comment.