From 9e653b88c6900714648fa3ac7e5be01f3cc539ff Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Wed, 19 Dec 2018 01:19:44 +0100 Subject: [PATCH] minor PHP code cleanup - no explicit property null assignments, less function calls, strict comparison --- Makefile | 7 +++++-- src/Event/ReplaceShortcodesEvent.php | 1 - src/EventContainer/EventContainer.php | 2 +- src/EventHandler/FilterRawEventHandler.php | 2 +- src/HandlerContainer/HandlerContainer.php | 2 +- src/Parser/WordpressParser.php | 2 +- src/Processor/Processor.php | 5 +++-- src/Processor/ProcessorContext.php | 12 ++++++------ src/Serializer/TextSerializer.php | 2 +- src/Shortcode/Shortcode.php | 4 ++-- 10 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index f9d14c8..6717312 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PHP ?= 7.2 +PHP ?= 7.3 composer-update: docker-compose run --rm composer composer config platform.php ${PHP} @@ -7,4 +7,7 @@ composer-update: test: docker-compose run --rm php-${PHP} php -v - docker-compose run --rm php-${PHP} php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist \ No newline at end of file + docker-compose run --rm php-${PHP} php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist +test-local: + php -v + php vendor/bin/phpunit diff --git a/src/Event/ReplaceShortcodesEvent.php b/src/Event/ReplaceShortcodesEvent.php index fd0d776..523a9d9 100644 --- a/src/Event/ReplaceShortcodesEvent.php +++ b/src/Event/ReplaceShortcodesEvent.php @@ -23,7 +23,6 @@ public function __construct($text, array $replacements, ShortcodeInterface $shor { $this->shortcode = $shortcode; $this->text = $text; - $this->result = null; $this->setReplacements($replacements); } diff --git a/src/EventContainer/EventContainer.php b/src/EventContainer/EventContainer.php index 2839ae2..484d76a 100644 --- a/src/EventContainer/EventContainer.php +++ b/src/EventContainer/EventContainer.php @@ -16,7 +16,7 @@ public function __construct() public function addListener($event, $handler) { - if(!in_array($event, Events::getEvents())) { + if(!\in_array($event, Events::getEvents(), true)) { throw new \InvalidArgumentException(sprintf('Unsupported event %s!', $event)); } diff --git a/src/EventHandler/FilterRawEventHandler.php b/src/EventHandler/FilterRawEventHandler.php index 0253eb0..ca5da3e 100644 --- a/src/EventHandler/FilterRawEventHandler.php +++ b/src/EventHandler/FilterRawEventHandler.php @@ -25,7 +25,7 @@ public function __construct(array $names) public function __invoke(FilterShortcodesEvent $event) { $parent = $event->getParent(); - if($parent && in_array($parent->getName(), $this->names)) { + if($parent && \in_array($parent->getName(), $this->names, true)) { $event->setShortcodes(array()); return; diff --git a/src/HandlerContainer/HandlerContainer.php b/src/HandlerContainer/HandlerContainer.php index 4066e3e..8bd3eb8 100644 --- a/src/HandlerContainer/HandlerContainer.php +++ b/src/HandlerContainer/HandlerContainer.php @@ -62,7 +62,7 @@ public function getNames() public function get($name) { - return $this->has($name) ? $this->handlers[$name] : ($this->default ?: null); + return $this->has($name) ? $this->handlers[$name] : $this->default; } public function has($name) diff --git a/src/Parser/WordpressParser.php b/src/Parser/WordpressParser.php index 48a6594..a579e6b 100644 --- a/src/Parser/WordpressParser.php +++ b/src/Parser/WordpressParser.php @@ -97,7 +97,7 @@ private static function parseParameters($text) $parameters[strtolower($match[3])] = stripcslashes($match[4]); } elseif(!empty($match[5])) { $parameters[strtolower($match[5])] = stripcslashes($match[6]); - } elseif(isset($match[7]) && strlen($match[7])) { + } elseif(isset($match[7]) && $match[7] !== '') { $parameters[stripcslashes($match[7])] = null; } elseif(isset($match[8])) { $parameters[stripcslashes($match[8])] = null; diff --git a/src/Processor/Processor.php b/src/Processor/Processor.php index 8f25abe..879c411 100644 --- a/src/Processor/Processor.php +++ b/src/Processor/Processor.php @@ -23,7 +23,8 @@ final class Processor implements ProcessorInterface /** @var EventContainerInterface */ private $eventContainer; - private $recursionDepth = null; // infinite recursion + /** @var int|null */ + private $recursionDepth; // infinite recursion private $maxIterations = 1; // one iteration private $autoProcessContent = true; // automatically process shortcode content @@ -144,7 +145,7 @@ private function processHandler(ParsedShortcodeInterface $parsed, ProcessorConte return mb_substr($state, 0, $offset, 'utf-8').$processed->getContent().mb_substr($state, $offset + $length, mb_strlen($state, 'utf-8'), 'utf-8'); } - private function processRecursion(ParsedShortcodeInterface $shortcode, ProcessorContext $context) + private function processRecursion(ProcessedShortcode $shortcode, ProcessorContext $context) { if ($this->autoProcessContent && null !== $shortcode->getContent()) { $context->recursionLevel++; diff --git a/src/Processor/ProcessorContext.php b/src/Processor/ProcessorContext.php index 2b57d1d..c75eed6 100644 --- a/src/Processor/ProcessorContext.php +++ b/src/Processor/ProcessorContext.php @@ -9,21 +9,21 @@ final class ProcessorContext { /** @var ShortcodeInterface */ - public $shortcode = null; + public $shortcode; /** @var ShortcodeInterface */ - public $parent = null; + public $parent; /** @var ProcessorInterface */ - public $processor = null; + public $processor; - public $textContent = null; + public $textContent; public $position = 0; public $namePosition = array(); public $text = ''; public $shortcodeText = ''; public $iterationNumber = 0; public $recursionLevel = 0; - public $offset = null; - public $baseOffset = null; + public $offset; + public $baseOffset; } diff --git a/src/Serializer/TextSerializer.php b/src/Serializer/TextSerializer.php index af89a78..1832962 100644 --- a/src/Serializer/TextSerializer.php +++ b/src/Serializer/TextSerializer.php @@ -56,7 +56,7 @@ private function serializeValue($value) $delimiter = $this->syntax->getParameterValueDelimiter(); $separator = $this->syntax->getParameterValueSeparator(); - return $separator.(preg_match('/^\w+$/us', $value) + return $separator.(preg_match('/^\w+$/u', $value) ? $value : $delimiter.$value.$delimiter); } diff --git a/src/Shortcode/Shortcode.php b/src/Shortcode/Shortcode.php index 5e9228b..99bcb2b 100644 --- a/src/Shortcode/Shortcode.php +++ b/src/Shortcode/Shortcode.php @@ -8,11 +8,11 @@ final class Shortcode extends AbstractShortcode implements ShortcodeInterface { public function __construct($name, array $parameters, $content, $bbCode = null) { - if(false === is_string($name) || (is_string($name) && !\mb_strlen($name))) { + if(false === is_string($name) || '' === $name) { throw new \InvalidArgumentException('Shortcode name must be a non-empty string!'); } - $isStringOrNull = function($value) { return is_string($value) || is_null($value); }; + $isStringOrNull = function($value) { return is_string($value) || null === $value; }; if(count(array_filter($parameters, $isStringOrNull)) !== count($parameters)) { throw new \InvalidArgumentException('Parameter values must be either string or empty (null)!'); }