Skip to content

Commit

Permalink
minor PHP code cleanup - no explicit property null assignments, less …
Browse files Browse the repository at this point in the history
…function calls, strict comparison
  • Loading branch information
thunderer committed Apr 17, 2019
1 parent ecbea3a commit 9e653b8
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHP ?= 7.2
PHP ?= 7.3

composer-update:
docker-compose run --rm composer composer config platform.php ${PHP}
Expand All @@ -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
docker-compose run --rm php-${PHP} php /app/vendor/bin/phpunit -c /app/phpunit.xml.dist
test-local:
php -v
php vendor/bin/phpunit
1 change: 0 additions & 1 deletion src/Event/ReplaceShortcodesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct($text, array $replacements, ShortcodeInterface $shor
{
$this->shortcode = $shortcode;
$this->text = $text;
$this->result = null;

$this->setReplacements($replacements);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EventContainer/EventContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/EventHandler/FilterRawEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/HandlerContainer/HandlerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/WordpressParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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++;
Expand Down
12 changes: 6 additions & 6 deletions src/Processor/ProcessorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Serializer/TextSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Shortcode/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)!');
}
Expand Down

0 comments on commit 9e653b8

Please sign in to comment.