diff --git a/Command/DebugFlagsCommand.php b/Command/DebugFlagsCommand.php index f9027b6..56aaf8c 100644 --- a/Command/DebugFlagsCommand.php +++ b/Command/DebugFlagsCommand.php @@ -10,24 +10,12 @@ class DebugFlagsCommand extends Command { - /** - * @var Toggle - */ - private $toggle; - - /** - * @var Toggle\ConditionBag - */ - private $conditionBag; - - public function __construct(Toggle $toggle, Toggle\ConditionBag $conditionBag) + public function __construct(private readonly Toggle $toggle, private readonly Toggle\ConditionBag $conditionBag) { parent::__construct(); - $this->toggle = $toggle; - $this->conditionBag = $conditionBag; } - protected function configure() + protected function configure(): void { $this ->setName('dzunke:feature_flags:debug') @@ -50,7 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $table->setStyle('borderless'); $table->setHeaders(['name', 'class']); foreach ($this->conditionBag as $name => $condition) { - $table->addRow([$name, get_class($condition)]); + $table->addRow([$name, $condition::class]); } $table->render(); @@ -67,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function renderFlagsTable(OutputInterface $output) + private function renderFlagsTable(OutputInterface $output): void { $flags = $this->toggle->getFlags(); if (empty($flags)) { diff --git a/Context.php b/Context.php index a20947f..5c0ebce 100644 --- a/Context.php +++ b/Context.php @@ -15,7 +15,7 @@ class Context * @param mixed $value * @return $this */ - public function set($name, $value) + public function set($name, $value): self { $this->context[(string)$name] = $value; @@ -34,7 +34,7 @@ public function get($name) /** * @return array */ - public function all() + public function all(): array { return $this->context; } @@ -42,7 +42,7 @@ public function all() /** * @return $this */ - public function clear() + public function clear(): self { $this->context = []; diff --git a/DZunkeFeatureFlagsBundle.php b/DZunkeFeatureFlagsBundle.php index f2f5a29..1236fe8 100644 --- a/DZunkeFeatureFlagsBundle.php +++ b/DZunkeFeatureFlagsBundle.php @@ -9,7 +9,7 @@ class DZunkeFeatureFlagsBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container); diff --git a/DependencyInjection/Compiler/Conditions.php b/DependencyInjection/Compiler/Conditions.php index 119826a..4b7defa 100644 --- a/DependencyInjection/Compiler/Conditions.php +++ b/DependencyInjection/Compiler/Conditions.php @@ -13,7 +13,7 @@ class Conditions implements CompilerPassInterface /** * @param ContainerBuilder $container */ - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (true !== $container->getParameterBag()->has('d_zunke_feature_flags.config')) { throw new \RuntimeException('No parameters with name \'d_zunke_feature_flags.config\' could be found'); @@ -28,7 +28,7 @@ public function process(ContainerBuilder $container) * @param array $config * @param ContainerBuilder $container */ - private function configureToggle(array $config, ContainerBuilder $container) + private function configureToggle(array $config, ContainerBuilder $container): void { $definition = $container->getDefinition('dz.feature_flags.toggle'); $definition->addMethodCall('setDefaultState', [$config['default']]); @@ -52,7 +52,7 @@ private function configureToggle(array $config, ContainerBuilder $container) /** * @param ContainerBuilder $container */ - private function fillConditionsBag(ContainerBuilder $container) + private function fillConditionsBag(ContainerBuilder $container): void { $taggedConditions = $container->findTaggedServiceIds('dz.feature_flags.toggle.condition'); @@ -76,7 +76,7 @@ private function fillConditionsBag(ContainerBuilder $container) * * @return Definition */ - private function createFlagDefinition($name, Reference $reference, $default) + private function createFlagDefinition($name, Reference $reference, $default): Definition { return new Definition( Flag::class, @@ -92,7 +92,7 @@ private function createFlagDefinition($name, Reference $reference, $default) * @param Definition $flag * @param array $config */ - private function processFlagConditions(Definition $flag, array $config) + private function processFlagConditions(Definition $flag, array $config): void { foreach($config as $condition => $optionalValues) { $flag->addMethodCall( diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c0b1654..47f1f0f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -3,6 +3,7 @@ namespace DZunke\FeatureFlagsBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -12,7 +13,7 @@ class Configuration implements ConfigurationInterface /** * @return TreeBuilder */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('d_zunke_feature_flags'); $rootNode = $treeBuilder->getRootNode(); @@ -30,9 +31,9 @@ public function getConfigTreeBuilder() } /** - * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition + * @return ArrayNodeDefinition|NodeDefinition */ - private function addFlags() + private function addFlags(): ArrayNodeDefinition|NodeDefinition { $treeBuilder = new TreeBuilder('flags'); $node = $treeBuilder->getRootNode(); diff --git a/DependencyInjection/DZunkeFeatureFlagsExtension.php b/DependencyInjection/DZunkeFeatureFlagsExtension.php index 1a9457e..3e27175 100644 --- a/DependencyInjection/DZunkeFeatureFlagsExtension.php +++ b/DependencyInjection/DZunkeFeatureFlagsExtension.php @@ -14,7 +14,7 @@ class DZunkeFeatureFlagsExtension extends Extension * @param array $configs * @param ContainerBuilder $container */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); diff --git a/EventListener/ContextCreator.php b/EventListener/ContextCreator.php index 9b1a788..8a6bef4 100644 --- a/EventListener/ContextCreator.php +++ b/EventListener/ContextCreator.php @@ -7,26 +7,19 @@ class ContextCreator { - /** - * @var Context - */ - private $context; - /** * @param Context $context */ - public function __construct(Context $context) + public function __construct(private readonly Context $context) { - $this->context = $context; } /** * @param RequestEvent $event */ - public function onKernelRequest(RequestEvent $event) + public function onKernelRequest(RequestEvent $event): void { $this->context->set('client_ip', $event->getRequest()->getClientIp()); $this->context->set('hostname', $event->getRequest()->getHost()); } - } diff --git a/Tests/ContextTest.php b/Tests/ContextTest.php index 3f321fd..a1132f6 100644 --- a/Tests/ContextTest.php +++ b/Tests/ContextTest.php @@ -8,7 +8,7 @@ class ContextTest extends TestCase { - public function testGetAndSet() + public function testGetAndSet(): void { $sut = new Context(); @@ -16,7 +16,7 @@ public function testGetAndSet() $this->assertSame('value', $sut->get('first')); } - public function testAll() + public function testAll(): void { $sut = new Context(); $sut->set('first', 'value'); @@ -25,7 +25,7 @@ public function testAll() $this->assertSame(1, count($sut->all())); } - public function testClear() + public function testClear(): void { $sut = new Context(); $sut->set('first', 'value'); @@ -36,4 +36,4 @@ public function testClear() $this->assertNull($sut->get('second')); } -} \ No newline at end of file +} diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index a53d01e..185a5cb 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -8,7 +8,7 @@ final class ConfigurationTest extends TestCase { - public function testDefaultConfigurationCouldBeLoaded() + public function testDefaultConfigurationCouldBeLoaded(): void { $container = new ContainerBuilder(); $extension = new DZunkeFeatureFlagsExtension(); @@ -21,4 +21,4 @@ public function testDefaultConfigurationCouldBeLoaded() $config ); } -} \ No newline at end of file +} diff --git a/Tests/Toggle/ConditionBagTest.php b/Tests/Toggle/ConditionBagTest.php index f9d64e3..68d684d 100644 --- a/Tests/Toggle/ConditionBagTest.php +++ b/Tests/Toggle/ConditionBagTest.php @@ -12,14 +12,14 @@ class ConditionBagTest extends TestCase { - public function testItImplementsClasses() + public function testItImplementsClasses(): void { $sut = new ConditionBag(); $this->assertInstanceOf(IteratorAggregate::class, $sut); $this->assertInstanceOf(Countable::class, $sut); } - public function testConditionBag() + public function testConditionBag(): void { $conditionMock = $this->createMock(ConditionInterface::class); $conditionMock->method('__toString')->willReturn('test_condition'); @@ -39,4 +39,4 @@ public function testConditionBag() $this->assertCount(0, $sut->remove('test_condition')); } -} \ No newline at end of file +} diff --git a/Tests/Toggle/Conditions/DateTest.php b/Tests/Toggle/Conditions/DateTest.php index a7233cc..c9aa953 100644 --- a/Tests/Toggle/Conditions/DateTest.php +++ b/Tests/Toggle/Conditions/DateTest.php @@ -14,34 +14,34 @@ class DateTest extends TestCase /** * @var string */ - const CURRENT_DATE = "2016-09-02T00:00:00Z"; + public const CURRENT_DATE = "2016-09-02T00:00:00Z"; /** * @var string */ - const FUTURE_DATE1 = "2020-12-25T12:00:00Z"; + public const FUTURE_DATE1 = "2020-12-25T12:00:00Z"; /** * @var string */ - const FUTURE_DATE2 = "2017-0-0T00:00:00Z"; + public const FUTURE_DATE2 = "2017-0-0T00:00:00Z"; /** * @var string */ - const PAST_DATE1 = "1970-01-01T00:00:00Z"; + public const PAST_DATE1 = "1970-01-01T00:00:00Z"; /** * @var string */ - const PAST_DATE2 = "2014-01-01T00:00:00Z"; + public const PAST_DATE2 = "2014-01-01T00:00:00Z"; /** * Returns an instance of Date with a known "current" date. * * @return Date */ - public function getInstanceOfDate() + public function getInstanceOfDate(): Date { $contextMock = $this->createMock(Context::class); @@ -56,7 +56,7 @@ public function getInstanceOfDate() return $date; } - public function testItExtendsCorrectly() + public function testItExtendsCorrectly(): void { $sut = $this->getInstanceOfDate(); @@ -64,14 +64,14 @@ public function testItExtendsCorrectly() $this->assertInstanceOf(ConditionInterface::class, $sut); } - public function testItReturnsTrueWithoutStartOrEndDate() + public function testItReturnsTrueWithoutStartOrEndDate(): void { $config = []; $sut = $this->getInstanceOfDate(); $this->assertTrue($sut->validate($config)); } - public function testItReturnsTrueWithPastStartDate() + public function testItReturnsTrueWithPastStartDate(): void { $sut = $this->getInstanceOfDate(); $this->assertTrue($sut->validate([ @@ -88,7 +88,7 @@ public function testItReturnsTrueWithPastStartDate() ])); } - public function testItReturnsTrueWithFutureEndDate() + public function testItReturnsTrueWithFutureEndDate(): void { $sut = $this->getInstanceOfDate(); $this->assertTrue($sut->validate([ @@ -105,7 +105,7 @@ public function testItReturnsTrueWithFutureEndDate() ])); } - public function testItReturnsTrueWhenBetweenPastStartAndFutureEndDate() + public function testItReturnsTrueWhenBetweenPastStartAndFutureEndDate(): void { $sut = $this->getInstanceOfDate(); $this->assertTrue($sut->validate([ @@ -126,7 +126,7 @@ public function testItReturnsTrueWhenBetweenPastStartAndFutureEndDate() ])); } - public function testToString() + public function testToString(): void { $sut = $this->getInstanceOfDate(); diff --git a/Tests/Toggle/Conditions/DeviceTest.php b/Tests/Toggle/Conditions/DeviceTest.php index 1315fc9..e22f714 100644 --- a/Tests/Toggle/Conditions/DeviceTest.php +++ b/Tests/Toggle/Conditions/DeviceTest.php @@ -32,7 +32,7 @@ public function setUp() : void } } - public function testItExtendsCorrectly() + public function testItExtendsCorrectly(): void { $sut = new Device($this->requestStackMock); @@ -40,14 +40,14 @@ public function testItExtendsCorrectly() $this->assertInstanceOf(ConditionInterface::class, $sut); } - public function testIsReturnsFalseItConfigIsIncorrect() + public function testIsReturnsFalseItConfigIsIncorrect(): void { $sut = new Device($this->requestStackMock); $this->assertFalse($sut->validate(null)); } - public function testItReturnsFalseIfUserAgentDoesNotExistInArray() + public function testItReturnsFalseIfUserAgentDoesNotExistInArray(): void { $headerBagMock = $this->createMock(HeaderBag::class); $headerBagMock->method('get')->with('User-Agent')->willReturn('Custom-User-Agent'); @@ -62,7 +62,7 @@ public function testItReturnsFalseIfUserAgentDoesNotExistInArray() $this->assertFalse($sut->validate([], null)); } - public function testItReturnsBoolWhenCallingValidateWithoutArgument() + public function testItReturnsBoolWhenCallingValidateWithoutArgument(): void { $headerBagMock = $this->createMock(HeaderBag::class); $headerBagMock->method('get')->with('User-Agent')->will($this->onConsecutiveCalls('Matched-User-Agent', 'Random-User-Agent')); @@ -83,7 +83,7 @@ public function testItReturnsBoolWhenCallingValidateWithoutArgument() $this->assertFalse($sut->validate($array, null)); } - public function testItReturnsBoolWhenCallingValidateWithArgument() + public function testItReturnsBoolWhenCallingValidateWithArgument(): void { $headerBagMock = $this->createMock(HeaderBag::class); $headerBagMock->method('get')->with('User-Agent')->willReturn('Custom-Agent-3', 'Random-User-Agent'); @@ -104,7 +104,7 @@ public function testItReturnsBoolWhenCallingValidateWithArgument() $this->assertFalse($sut->validate($array, 'second-agents')); } - public function testToString() + public function testToString(): void { $sut = new Device($this->requestStackMock); diff --git a/Tests/Toggle/Conditions/HostnameTest.php b/Tests/Toggle/Conditions/HostnameTest.php index 9e001d8..1eb7c70 100644 --- a/Tests/Toggle/Conditions/HostnameTest.php +++ b/Tests/Toggle/Conditions/HostnameTest.php @@ -11,7 +11,7 @@ class HostnameTest extends TestCase { - public function testItExtendsCorrectly() + public function testItExtendsCorrectly(): void { $sut = new Hostname(); @@ -19,7 +19,7 @@ public function testItExtendsCorrectly() $this->assertInstanceOf(ConditionInterface::class, $sut); } - public function testItReturnsBoolean() + public function testItReturnsBoolean(): void { $contextMock = $this->createMock(Context::class); $contextMock->method('get')->will($this->onConsecutiveCalls('myhostname', 'thirdhostname')); @@ -37,7 +37,7 @@ public function testItReturnsBoolean() $this->assertTrue($sut->validate($array)); } - public function testToString() + public function testToString(): void { $sut = new Hostname(); diff --git a/Tests/Toggle/Conditions/IpAddressTest.php b/Tests/Toggle/Conditions/IpAddressTest.php index 5412b20..3405eeb 100644 --- a/Tests/Toggle/Conditions/IpAddressTest.php +++ b/Tests/Toggle/Conditions/IpAddressTest.php @@ -11,7 +11,7 @@ class IpAddressTest extends TestCase { - public function testItExtendsCorrectly() + public function testItExtendsCorrectly(): void { $sut = new IpAddress(); @@ -19,7 +19,7 @@ public function testItExtendsCorrectly() $this->assertInstanceOf(ConditionInterface::class, $sut); } - public function testItReturnsTrue() + public function testItReturnsTrue(): void { $contextMock = $this->createMock(Context::class); $contextMock->method('get')->will($this->onConsecutiveCalls('127.0.0.1', '169.168.1.12')); @@ -38,7 +38,7 @@ public function testItReturnsTrue() $this->assertFalse($sut->validate($array)); } - public function testToString() + public function testToString(): void { $sut = new IpAddress(); diff --git a/Tests/Toggle/Conditions/PercentageTest.php b/Tests/Toggle/Conditions/PercentageTest.php index 6753712..3faeca9 100644 --- a/Tests/Toggle/Conditions/PercentageTest.php +++ b/Tests/Toggle/Conditions/PercentageTest.php @@ -26,7 +26,7 @@ public function setUp() : void } - public function testItExtendsCorrectly() + public function testItExtendsCorrectly(): void { $sut = new Percentage($this->requestStackMock); @@ -34,7 +34,7 @@ public function testItExtendsCorrectly() $this->assertInstanceOf(ConditionInterface::class, $sut); } - public function testItThrowsExceptionWhenPercentageIsNotSet() + public function testItThrowsExceptionWhenPercentageIsNotSet(): void { $this->expectException(Exception::class); @@ -42,7 +42,7 @@ public function testItThrowsExceptionWhenPercentageIsNotSet() $sut->validate([], 'nothing'); } - public function testItReturnsTrueWhenCookieIsAlreadySet() + public function testItReturnsTrueWhenCookieIsAlreadySet(): void { $parameterBagMock = $this->createMock(ParameterBag::class); $parameterBagMock->method('has')->willReturn(true); @@ -60,7 +60,7 @@ public function testItReturnsTrueWhenCookieIsAlreadySet() ])); } - public function testItReturnsBoolWhenCookieIsNotSet() + public function testItReturnsBoolWhenCookieIsNotSet(): void { $parameterBagMock = $this->createMock(ParameterBag::class); $parameterBagMock->method('has')->willReturn(true); @@ -75,7 +75,7 @@ public function testItReturnsBoolWhenCookieIsNotSet() self::assertIsBool($sut->validate(['percentage' => 3])); } - public function testToString() + public function testToString(): void { $sut = new Percentage($this->requestStackMock); diff --git a/Tests/Toggle/FlagTest.php b/Tests/Toggle/FlagTest.php index 5c08f89..a05aeb6 100644 --- a/Tests/Toggle/FlagTest.php +++ b/Tests/Toggle/FlagTest.php @@ -11,7 +11,7 @@ class FlagTest extends TestCase { - public function testIsActiveReturnsBool() + public function testIsActiveReturnsBool(): void { $conditionBagMock = $this->createMock(ConditionBag::class); @@ -31,7 +31,7 @@ public function testIsActiveReturnsBool() $this->assertFalse($sut->isActive()); } - public function testIsActiveReturnsDefaultIfEmptyArray() + public function testIsActiveReturnsDefaultIfEmptyArray(): void { $conditionBagMock = $this->createMock(ConditionBag::class); @@ -40,7 +40,7 @@ public function testIsActiveReturnsDefaultIfEmptyArray() $this->assertTrue($sut->isActive('argument')); } - public function testGetConfigReturnsArray() + public function testGetConfigReturnsArray(): void { $conditionBagMock = $this->createMock(ConditionBag::class); @@ -49,7 +49,7 @@ public function testGetConfigReturnsArray() self::assertIsArray($sut->getConfig()); } - public function testAddConditionThrowsException() + public function testAddConditionThrowsException(): void { $this->expectException(InvalidArgumentException::class); @@ -63,7 +63,7 @@ public function testAddConditionThrowsException() $this->assertInstanceOf(Flag::class, $return); } - public function testToString() + public function testToString(): void { $conditionBagMock = $this->createMock(ConditionBag::class); @@ -72,4 +72,4 @@ public function testToString() $this->assertSame('MySpecialFeature', (string) $sut); } -} \ No newline at end of file +} diff --git a/Tests/ToggleTest.php b/Tests/ToggleTest.php index e85310b..2419861 100644 --- a/Tests/ToggleTest.php +++ b/Tests/ToggleTest.php @@ -8,14 +8,14 @@ class ToggleTest extends TestCase { - public function testDefaultStateIsTrue() + public function testDefaultStateIsTrue(): void { $sut = new Toggle(); $this->assertTrue($sut->isActive('test')); } - public function testDefaultState() + public function testDefaultState(): void { $sut = new Toggle(); @@ -23,7 +23,7 @@ public function testDefaultState() $this->assertInstanceOf(Toggle::class, $sut->setDefaultState(false)); } - public function testGetAndAddFlag() + public function testGetAndAddFlag(): void { $flagMock = self::createMock(Toggle\Flag::class); $flagMock->method('__toString')->willReturn('test_flag'); @@ -35,7 +35,7 @@ public function testGetAndAddFlag() $this->assertSame($flagMock, $sut->getFlag('test_flag')); } - public function testIsActive() + public function testIsActive(): void { $flagMock = $this->createMock(Toggle\Flag::class); $flagMock->method('__toString')->willReturn('MySpecialFeature'); @@ -47,4 +47,4 @@ public function testIsActive() $this->assertTrue($sut->isActive('MySpecialFeature')); $this->assertFalse($sut->isActive('MySpecialFeature')); } -} \ No newline at end of file +} diff --git a/Tests/Twig/FeatureExtensionTest.php b/Tests/Twig/FeatureExtensionTest.php index 076ed89..9af4c90 100644 --- a/Tests/Twig/FeatureExtensionTest.php +++ b/Tests/Twig/FeatureExtensionTest.php @@ -10,7 +10,7 @@ class FeatureExtensionTest extends TestCase { - public function testGetFunctions() + public function testGetFunctions(): void { $toggleMock = $this->createMock(Toggle::class); @@ -21,7 +21,7 @@ public function testGetFunctions() $this->assertInstanceOf(TwigFunction::class, reset($functions)); } - public function testGetName() + public function testGetName(): void { $toggleMock = $this->createMock(Toggle::class); @@ -30,4 +30,4 @@ public function testGetName() $this->assertSame('d_zunke_feature_extension', $sut->getName()); } -} \ No newline at end of file +} diff --git a/Toggle.php b/Toggle.php index 218b260..89788c2 100644 --- a/Toggle.php +++ b/Toggle.php @@ -21,7 +21,7 @@ class Toggle * @param bool $defaultState * @return $this */ - public function setDefaultState($defaultState) + public function setDefaultState($defaultState): self { $this->defaultState = (bool)$defaultState; @@ -32,7 +32,7 @@ public function setDefaultState($defaultState) * @param Flag $flag * @return $this */ - public function addFlag(Flag $flag) + public function addFlag(Flag $flag): self { $this->flags[(string)$flag] = $flag; @@ -43,7 +43,7 @@ public function addFlag(Flag $flag) * @param $name * @return Flag|null */ - public function getFlag($name) + public function getFlag($name): ?Flag { if (isset($this->flags[$name])) { return $this->flags[$name]; @@ -55,7 +55,7 @@ public function getFlag($name) /** * @return Toggle\Flag[] */ - public function getFlags() + public function getFlags(): array { return $this->flags; } @@ -65,7 +65,7 @@ public function getFlags() * @param array $arguments * @return bool */ - public function isActive($flag, $arguments = null) + public function isActive($flag, $arguments = null): bool { if (!isset($this->flags[$flag])) { return $this->defaultState; diff --git a/Toggle/ConditionBag.php b/Toggle/ConditionBag.php index 2d29923..a5f8304 100644 --- a/Toggle/ConditionBag.php +++ b/Toggle/ConditionBag.php @@ -10,7 +10,7 @@ class ConditionBag implements \IteratorAggregate, \Countable /** * @var ConditionInterface[] */ - protected $conditions = []; + protected array $conditions = []; /** * @return \ArrayIterator @@ -31,7 +31,7 @@ public function count(): int /** * @return Conditions\ConditionInterface[] */ - public function all() + public function all(): array { return $this->conditions; } @@ -40,7 +40,7 @@ public function all() * @param array $conditions * @return $this */ - public function add(array $conditions) + public function add(array $conditions): self { foreach ($conditions as $condition) { $this->set((string)$condition, $condition); @@ -54,7 +54,7 @@ public function add(array $conditions) * @param ConditionInterface $condition * @return $this */ - public function set($name, ConditionInterface $condition) + public function set(string $name, ConditionInterface $condition): self { $this->conditions[$name] = $condition; @@ -62,18 +62,18 @@ public function set($name, ConditionInterface $condition) } /** - * @param $name + * @param string $name * @return ConditionInterface|null */ - public function get($name) + public function get(string $name): ?ConditionInterface { - return isset($this->conditions[$name]) ? $this->conditions[$name] : null; + return $this->conditions[$name] ?? null; } /** * @return array */ - public function keys() + public function keys(): array { return array_keys($this->conditions); } @@ -82,7 +82,7 @@ public function keys() * @param string $name * @return bool */ - public function has($name) + public function has(string $name): bool { return array_key_exists($name, $this->conditions); } @@ -91,7 +91,7 @@ public function has($name) * @param string $name * @return $this */ - public function remove($name) + public function remove(string $name): self { unset($this->conditions[$name]); diff --git a/Toggle/Conditions/AbstractCondition.php b/Toggle/Conditions/AbstractCondition.php index a42c8e5..0af556d 100644 --- a/Toggle/Conditions/AbstractCondition.php +++ b/Toggle/Conditions/AbstractCondition.php @@ -10,12 +10,12 @@ abstract class AbstractCondition implements ConditionInterface * @var Context */ protected $context; - + /** * @param Context $context * @return $this */ - public function setContext(Context $context) + public function setContext(Context $context): static { $this->context = $context; diff --git a/Toggle/Conditions/ConditionInterface.php b/Toggle/Conditions/ConditionInterface.php index 7433e53..7e1f8b4 100644 --- a/Toggle/Conditions/ConditionInterface.php +++ b/Toggle/Conditions/ConditionInterface.php @@ -6,10 +6,10 @@ interface ConditionInterface { - public function __toString(); + public function __toString(): string; - public function setContext(Context $context); + public function setContext(Context $context): static; - public function validate($config, $argument = null); + public function validate($config, $argument = null): bool; } diff --git a/Toggle/Conditions/Date.php b/Toggle/Conditions/Date.php index 6edbe41..0243920 100644 --- a/Toggle/Conditions/Date.php +++ b/Toggle/Conditions/Date.php @@ -8,7 +8,7 @@ class Date extends AbstractCondition implements ConditionInterface { /** * Current date, in DateTime format. - * + * * @see http://php.net/manual/en/class.datetime.php * * @var string @@ -20,7 +20,7 @@ class Date extends AbstractCondition implements ConditionInterface * @param null $argument * @return bool */ - public function validate($config, $argument = null) + public function validate($config, $argument = null): bool { $currentDate = new DateTime($this->currentDate); $startDate = !empty($config['start_date']) ? new DateTime($config['start_date']) : $currentDate; @@ -31,7 +31,7 @@ public function validate($config, $argument = null) /** * @return string */ - public function __toString() + public function __toString(): string { return 'date'; } diff --git a/Toggle/Conditions/Device.php b/Toggle/Conditions/Device.php index 565b380..a8d6895 100644 --- a/Toggle/Conditions/Device.php +++ b/Toggle/Conditions/Device.php @@ -26,7 +26,7 @@ public function __construct(RequestStack $request) * @param null $argument * @return bool */ - public function validate($config, $argument = null) + public function validate($config, $argument = null): bool { if (!$this->validateConfig($config)) { return false; @@ -36,22 +36,21 @@ public function validate($config, $argument = null) if (is_null($argument)) { foreach ($config as $name => $regex) { - if (preg_match($regex, $userAgent)) { + if (preg_match($regex, (string) $userAgent)) { return true; } } } elseif (!is_null($argument) && array_key_exists($argument, $config)) { - return (bool)preg_match($config[$argument], $userAgent); + return (bool)preg_match($config[$argument], (string) $userAgent); } return false; } /** - * @param mixed $config * @return bool */ - private function validateConfig($config) + private function validateConfig(mixed $config): bool { if (!is_array($config)) { return false; @@ -63,7 +62,7 @@ private function validateConfig($config) /** * @return string */ - public function __toString() + public function __toString(): string { return 'device'; } diff --git a/Toggle/Conditions/Hostname.php b/Toggle/Conditions/Hostname.php index 7eeef66..183f80c 100644 --- a/Toggle/Conditions/Hostname.php +++ b/Toggle/Conditions/Hostname.php @@ -10,7 +10,7 @@ class Hostname extends AbstractCondition implements ConditionInterface * @param null $argument * @return bool */ - public function validate($config, $argument = null) + public function validate($config, $argument = null): bool { return in_array( $this->context->get('hostname'), @@ -21,7 +21,7 @@ public function validate($config, $argument = null) /** * @return string */ - public function __toString() + public function __toString(): string { return 'hostname'; } diff --git a/Toggle/Conditions/IpAddress.php b/Toggle/Conditions/IpAddress.php index 3007d01..046ec3a 100644 --- a/Toggle/Conditions/IpAddress.php +++ b/Toggle/Conditions/IpAddress.php @@ -10,7 +10,7 @@ class IpAddress extends AbstractCondition implements ConditionInterface * @param null $argument * @return bool */ - public function validate($config, $argument = null) + public function validate($config, $argument = null): bool { return in_array( $this->context->get('client_ip'), @@ -21,7 +21,7 @@ public function validate($config, $argument = null) /** * @return string */ - public function __toString() + public function __toString(): string { return 'ip_address'; } diff --git a/Toggle/Conditions/Percentage.php b/Toggle/Conditions/Percentage.php index f8da3e1..122b82d 100644 --- a/Toggle/Conditions/Percentage.php +++ b/Toggle/Conditions/Percentage.php @@ -8,17 +8,14 @@ class Percentage extends AbstractCondition implements ConditionInterface { - const BASIC_COOKIE_NAME = '84a0b3f187a1d3bfefbb51d4b93074b1e5d9102a'; + public const BASIC_COOKIE_NAME = '84a0b3f187a1d3bfefbb51d4b93074b1e5d9102a'; - const BASIC_PERCENTAGE = 100; + public const BASIC_PERCENTAGE = 100; - const BASIC_LIFETIME = 86400; + public const BASIC_LIFETIME = 86400; - private RequestStack $requestStack; - - public function __construct(RequestStack $requestStack) + public function __construct(private readonly RequestStack $requestStack) { - $this->requestStack = $requestStack; } /** @@ -27,7 +24,7 @@ public function __construct(RequestStack $requestStack) * @return bool * @throws \Exception */ - public function validate($config, $argument = null) + public function validate($config, $argument = null): bool { $config = $this->formatConfig($config); @@ -39,7 +36,7 @@ public function validate($config, $argument = null) setcookie( $config['cookie'], $value, - time() + $config['lifetime'] + ['expires' => time() + $config['lifetime']] ); return (bool)$value; @@ -65,7 +62,7 @@ private function formatConfig($config) /** * @return int */ - private function generateRandomNumber() + private function generateRandomNumber(): int { return 100 * (mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax()); } @@ -73,7 +70,7 @@ private function generateRandomNumber() /** * @return string */ - public function __toString() + public function __toString(): string { return 'percentage'; } diff --git a/Toggle/Flag.php b/Toggle/Flag.php index fda570e..7c9feda 100644 --- a/Toggle/Flag.php +++ b/Toggle/Flag.php @@ -2,23 +2,13 @@ namespace DZunke\FeatureFlagsBundle\Toggle; -class Flag +class Flag implements \Stringable { - /** - * @var string - */ - private $name; - /** * @var bool */ private $active; - /** - * @var ConditionBag - */ - private $conditions; - /** * @var array */ @@ -28,18 +18,17 @@ class Flag * @param $name * @param ConditionBag $conditions * @param bool $defaultState + * @param string $name */ - public function __construct($name, ConditionBag $conditions, $defaultState = true) + public function __construct(private $name, private readonly ConditionBag $conditions, $defaultState = true) { - $this->name = $name; - $this->conditions = $conditions; $this->active = (bool)$defaultState; } /** * @return string */ - public function __toString() + public function __toString(): string { return $this->name; } @@ -49,7 +38,7 @@ public function __toString() * @param $config * @return $this */ - public function addCondition($name, $config) + public function addCondition($name, $config): self { if (!$this->conditions->has($name)) { throw new \InvalidArgumentException( @@ -65,7 +54,7 @@ public function addCondition($name, $config) /** * @return array */ - public function getConfig() + public function getConfig(): array { return $this->config; } @@ -74,7 +63,7 @@ public function getConfig() * @param array $arguments * @return bool */ - public function isActive($arguments = null) + public function isActive($arguments = null): bool { $actual = $this->active; diff --git a/Twig/FeatureExtension.php b/Twig/FeatureExtension.php index 92a8994..6a5ef53 100644 --- a/Twig/FeatureExtension.php +++ b/Twig/FeatureExtension.php @@ -28,7 +28,7 @@ public function __construct(Toggle $toggle) public function getFunctions(): array { return [ - new TwigFunction('has_feature', [$this, 'checkFeature'], ['is_safe' => ['html']]), + new TwigFunction('has_feature', $this->checkFeature(...), ['is_safe' => ['html']]), ]; } @@ -37,12 +37,12 @@ public function getFunctions(): array * @param array $arguments * @return bool */ - public function checkFeature($name, $arguments = null) + public function checkFeature($name, $arguments = null): bool { return $this->toggle->isActive($name, $arguments); } - public function getName() + public function getName(): string { return 'd_zunke_feature_extension'; }