Skip to content

Commit

Permalink
Improvement, reduce opcode conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Oct 12, 2023
1 parent a6e72cd commit c04e20d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
8 changes: 6 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
use PhpCsFixer\Finder;

$finder = Finder::create()
->in(__DIR__);
->in([__DIR__, __DIR__ . DIRECTORY_SEPARATOR . 'tests']);

$rules = [
'psr_autoloading' => false,
'@PSR2' => true,
'phpdoc_order' => true,
'ordered_imports' => true,
'native_function_invocation' => [
'include' => ['@internal'],
'exclude' => ['file_put_contents']
]
];

$cacheDir = getenv('HOME') ? getenv('HOME') : __DIR__;
$cacheDir = \getenv('HOME') ? \getenv('HOME') : __DIR__;

$config = new Config();

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
},
"scripts": {
"test": "phpunit",
"cs-fix": "php-cs-fixer fix . -vv || true",
"cs-lint": "php-cs-fixer fix --diff --stop-on-violation --verbose --dry-run",
"cs-fix": "php-cs-fixer fix . -vv --allow-risky=yes || true",
"cs-lint": "php-cs-fixer fix --diff --stop-on-violation --verbose --allow-risky=yes --dry-run",
"validate-gitattributes": "lean-package-validator validate"
},
"extra": {
Expand Down
52 changes: 26 additions & 26 deletions src/Rs/Json/Pointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Pointer
*/
public function __construct($json)
{
$this->json = json_decode($json);
$this->json = \json_decode($json);

if (json_last_error() !== JSON_ERROR_NONE) {
if (\json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidJsonException('Cannot operate on invalid Json.');
}

Expand All @@ -49,17 +49,17 @@ public function __construct($json)
public function get($pointer)
{
if ($pointer === '') {
$output = json_encode($this->json, JSON_UNESCAPED_UNICODE);
$output = \json_encode($this->json, JSON_UNESCAPED_UNICODE);
// workaround for https://bugs.php.net/bug.php?id=46600
return str_replace('"_empty_"', '""', $output);
return \str_replace('"_empty_"', '""', $output);
}

$this->validatePointer($pointer);

$this->pointer = $pointer;

$plainPointerParts = array_slice(
array_map('urldecode', explode('/', $pointer)),
$plainPointerParts = \array_slice(
\array_map('urldecode', \explode('/', $pointer)),
1
);
return $this->traverse($this->json, $this->evaluatePointerParts($plainPointerParts));
Expand All @@ -83,38 +83,38 @@ public function getPointer()
*/
private function traverse(&$json, array $pointerParts)
{
$pointerPart = array_shift($pointerParts);
$pointerPart = \array_shift($pointerParts);

if (is_array($json) && isset($json[$pointerPart])) {
if (count($pointerParts) === 0) {
if (\is_array($json) && isset($json[$pointerPart])) {
if (\count($pointerParts) === 0) {
return $json[$pointerPart];
}
if ((is_array($json[$pointerPart]) || is_object($json[$pointerPart])) && is_array($pointerParts)) {
if ((\is_array($json[$pointerPart]) || \is_object($json[$pointerPart])) && \is_array($pointerParts)) {
return $this->traverse($json[$pointerPart], $pointerParts);
}
} elseif (is_object($json) && in_array($pointerPart, array_keys(get_object_vars($json)))) {
if (count($pointerParts) === 0) {
} elseif (\is_object($json) && \in_array($pointerPart, \array_keys(\get_object_vars($json)))) {
if (\count($pointerParts) === 0) {
return $json->{$pointerPart};
}
if ((is_object($json->{$pointerPart}) || is_array($json->{$pointerPart})) && is_array($pointerParts)) {
if ((\is_object($json->{$pointerPart}) || \is_array($json->{$pointerPart})) && \is_array($pointerParts)) {
return $this->traverse($json->{$pointerPart}, $pointerParts);
}
} elseif (is_object($json) && empty($pointerPart) && array_key_exists('_empty_', get_object_vars($json))) {
} elseif (\is_object($json) && empty($pointerPart) && \array_key_exists('_empty_', \get_object_vars($json))) {
$pointerPart = '_empty_';
if (count($pointerParts) === 0) {
if (\count($pointerParts) === 0) {
return $json->{$pointerPart};
}
if ((is_object($json->{$pointerPart}) || is_array($json->{$pointerPart})) && is_array($pointerParts)) {
if ((\is_object($json->{$pointerPart}) || \is_array($json->{$pointerPart})) && \is_array($pointerParts)) {
return $this->traverse($json->{$pointerPart}, $pointerParts);
}
} elseif ($pointerPart === self::LAST_ARRAY_ELEMENT_CHAR && is_array($json)) {
return end($json);
} elseif (is_array($json) && count($json) < $pointerPart) {
} elseif ($pointerPart === self::LAST_ARRAY_ELEMENT_CHAR && \is_array($json)) {
return \end($json);
} elseif (\is_array($json) && \count($json) < $pointerPart) {
// Do nothing, let Exception bubble up
} elseif (is_array($json) && array_key_exists($pointerPart, $json) && $json[$pointerPart] === null) {
} elseif (\is_array($json) && \array_key_exists($pointerPart, $json) && $json[$pointerPart] === null) {
return $json[$pointerPart];
}
$exceptionMessage = sprintf(
$exceptionMessage = \sprintf(
"Json Pointer '%s' references a nonexistent value",
$this->getPointer()
);
Expand All @@ -126,7 +126,7 @@ private function traverse(&$json, array $pointerParts)
*/
private function isWalkableJson()
{
if ($this->json !== null && (is_array($this->json) || $this->json instanceof \stdClass)) {
if ($this->json !== null && (\is_array($this->json) || $this->json instanceof \stdClass)) {
return true;
}
return false;
Expand All @@ -138,11 +138,11 @@ private function isWalkableJson()
*/
private function validatePointer($pointer)
{
if ($pointer !== '' && !is_string($pointer)) {
if ($pointer !== '' && !\is_string($pointer)) {
throw new InvalidPointerException('Pointer is not a string');
}

$firstPointerCharacter = substr($pointer, 0, 1);
$firstPointerCharacter = \substr($pointer, 0, 1);

if ($firstPointerCharacter !== self::POINTER_CHAR) {
throw new InvalidPointerException('Pointer starts with invalid character');
Expand All @@ -160,8 +160,8 @@ private function evaluatePointerParts(array $pointerParts)
$evaluations = array('/', '~');

$parts = array();
array_filter($pointerParts, function ($v) use (&$parts, &$searchables, &$evaluations) {
return $parts[] = str_replace($searchables, $evaluations, $v);
\array_filter($pointerParts, function ($v) use (&$parts, &$searchables, &$evaluations) {
return $parts[] = \str_replace($searchables, $evaluations, $v);
});
return $parts;
}
Expand Down

0 comments on commit c04e20d

Please sign in to comment.