Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /** * Sandro Keil (https://sandro-keil.de) * * @link http://github.com/sandr..

Decoded Output download

<?php
/**
 * Sandro Keil (https://sandro-keil.de)
 *
 * @link      http://github.com/sandrokeil/php-to-zephir for the canonical source repository
 * @copyright Copyright (c) 2018 Sandro Keil
 * @license   http://github.com/sandrokeil/php-to-zephir/blob/master/LICENSE.md New BSD License
 */
declare(strict_types=1);

namespace PhpToZephir\PhpParser\NodeVisitor;

use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\PrettyPrinterAbstract;

class SortNamespaceClasses extends NodeVisitorAbstract
{
    private $namespaces = [];
    private $currentNamespace = '';

    public function enterNode(Node $node)
    {
        if ($node instanceof Node\Stmt\Namespace_
            && ! isset($this->namespaces[$node->name->toString()])
        ) {
            $this->namespaces[$node->name->toString()] = [];
            $this->currentNamespace = $node->name->toString();
        }

        if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
            $this->namespaces[$this->currentNamespace][$node->namespacedName->toString()] = $node;

            return NodeTraverser::DONT_TRAVERSE_CHILDREN;
        }
    }

    public function getNamespaces(): array
    {
        return $this->namespaces;
    }

    public function printSortedNamespaces(PrettyPrinterAbstract $printer): string
    {
        $code = '';

        foreach ($this->namespaces as $namespace => &$cls) {
            usort($cls, [self::class, 'sortNamespaces']);
            $namespaceNode = new Node\Stmt\Namespace_(new Node\Name($namespace), $cls);
            $code .= $printer->prettyPrint([$namespaceNode]) . PHP_EOL;
        }

        return $code;
    }

    public static function sortNamespaces(Node\Stmt\ClassLike $a, Node\Stmt\ClassLike $b): int
    {
        $aExtends = empty($a->extends);
        $bExtends = empty($b->extends);

        if ($aExtends && $bExtends) {
            return 0;
        }
        if ($aExtends && ! $bExtends) {
            return -1;
        }
        if (! $aExtends && $bExtends) {
            return 1;
        }
        $aNamespace = $a->namespacedName->toString();
        $bNamespace = $b->namespacedName->toString();

        if (! empty(array_filter($a->extends, function (Node\Name\FullyQualified $node) use ($bNamespace) {
            return $node->toString() === $bNamespace;
        }))) {
            return 1;
        }

        if (! empty(array_filter($b->extends, function (Node\Name\FullyQualified $node) use ($aNamespace) {
            return $node->toString() === $aNamespace;
        }))) {
            return -1;
        }

        return 0;
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php
/**
 * Sandro Keil (https://sandro-keil.de)
 *
 * @link      http://github.com/sandrokeil/php-to-zephir for the canonical source repository
 * @copyright Copyright (c) 2018 Sandro Keil
 * @license   http://github.com/sandrokeil/php-to-zephir/blob/master/LICENSE.md New BSD License
 */
declare(strict_types=1);

namespace PhpToZephir\PhpParser\NodeVisitor;

use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\PrettyPrinterAbstract;

class SortNamespaceClasses extends NodeVisitorAbstract
{
    private $namespaces = [];
    private $currentNamespace = '';

    public function enterNode(Node $node)
    {
        if ($node instanceof Node\Stmt\Namespace_
            && ! isset($this->namespaces[$node->name->toString()])
        ) {
            $this->namespaces[$node->name->toString()] = [];
            $this->currentNamespace = $node->name->toString();
        }

        if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
            $this->namespaces[$this->currentNamespace][$node->namespacedName->toString()] = $node;

            return NodeTraverser::DONT_TRAVERSE_CHILDREN;
        }
    }

    public function getNamespaces(): array
    {
        return $this->namespaces;
    }

    public function printSortedNamespaces(PrettyPrinterAbstract $printer): string
    {
        $code = '';

        foreach ($this->namespaces as $namespace => &$cls) {
            usort($cls, [self::class, 'sortNamespaces']);
            $namespaceNode = new Node\Stmt\Namespace_(new Node\Name($namespace), $cls);
            $code .= $printer->prettyPrint([$namespaceNode]) . PHP_EOL;
        }

        return $code;
    }

    public static function sortNamespaces(Node\Stmt\ClassLike $a, Node\Stmt\ClassLike $b): int
    {
        $aExtends = empty($a->extends);
        $bExtends = empty($b->extends);

        if ($aExtends && $bExtends) {
            return 0;
        }
        if ($aExtends && ! $bExtends) {
            return -1;
        }
        if (! $aExtends && $bExtends) {
            return 1;
        }
        $aNamespace = $a->namespacedName->toString();
        $bNamespace = $b->namespacedName->toString();

        if (! empty(array_filter($a->extends, function (Node\Name\FullyQualified $node) use ($bNamespace) {
            return $node->toString() === $bNamespace;
        }))) {
            return 1;
        }

        if (! empty(array_filter($b->extends, function (Node\Name\FullyQualified $node) use ($aNamespace) {
            return $node->toString() === $aNamespace;
        }))) {
            return -1;
        }

        return 0;
    }
}

Function Calls

None

Variables

None

Stats

MD5 bee7a350f4dff406b695a4773dc937c5
Eval Count 0
Decode Time 112 ms