Skip to content

Commit

Permalink
Add nice UI
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-rafalko committed May 26, 2024
1 parent a500e45 commit a58fa17
Show file tree
Hide file tree
Showing 17 changed files with 929 additions and 257 deletions.
10 changes: 9 additions & 1 deletion app/assets/css/app.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind utilities;

#clickable-nodes-code a.active-node {
color: forestgreen;
}

pre code.hljs {
border-radius: .6em;
}
15 changes: 4 additions & 11 deletions app/assets/js/app/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,10 @@ export function initAstEditor() {
: [
'<?php',
'',
'declare(strict_types=1);',
'',
'namespace Infected;',
'',
'class SourceClass',
'function add(int $a, int $b): int',
'{',
' public function add(int $a, int $b): int',
' {',
' return $a + $b;',
' }',
'}'
' return $a + $b;',
'}',
].join('\n');

const editorNode = document.getElementById('editor-code-ast');
Expand Down Expand Up @@ -306,7 +299,7 @@ function initCodeEditor() {
return;
}

const codeEditor = monaco.editor.create(editorNode, {
return monaco.editor.create(editorNode, {
minimap: {
enabled: false
},
Expand Down
2 changes: 2 additions & 0 deletions app/assets/js/ast.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '../css/app.css';

import {initAstEditor} from "./app/editor";

initAstEditor();
31 changes: 31 additions & 0 deletions app/migrations/Version20240521220725.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

Expand Down
1 change: 1 addition & 0 deletions app/phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
- tests/
excludePaths:
- src/Html/Ansi/*
- src/PhpParser/*
ignoreErrors:
- '#Access to an undefined property Symfony\\Component\\Validator\\Constraint::\$(.*?)#'
- '#^Property App\\Entity\\(.*):\$id is never written, only read.$#'
Expand Down
33 changes: 16 additions & 17 deletions app/src/Controller/AstController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,17 @@

namespace App\Controller;

use App\Code\CodeSanitizer;
use App\Entity\AstRun;
use App\Entity\Example;
use App\Form\CreateAstRunType;
use App\Form\CreateExampleType;
use App\Html\Ansi\AnsiToHtmlConverter;
use App\Html\Ansi\InfectionAnsiHtmlTheme;
use App\Infection\ConfigBuilder;
use App\Infection\Runner;
use App\PhpParser\ClickablePrinter;
use App\PhpParser\NodeResolver\FocusedNodeResolver;
use App\PhpParser\SimpleNodeDumper;
use App\PhpParser\SimplePhpParser;
use App\Repository\AstRunRepository;
use App\Repository\ExampleRepository;
use App\Request\CreateAstRunRequest;
use App\Request\CreateExampleRequest;
use Doctrine\ORM\EntityManagerInterface;
use Hashids\Hashids;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function is_int;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
Expand All @@ -67,7 +55,10 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\UseUse;
use function is_int;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class AstController extends AbstractController
{
Expand All @@ -91,7 +82,7 @@ public function index(): Response
]);
}

#[Route('/r', name: 'app_ast_create', methods: ['POST'])]
#[Route('/ast', name: 'app_ast_create', methods: ['POST'])]
public function createExample(Request $request, EntityManagerInterface $em): Response
{
$createAstRunRequest = new CreateAstRunRequest();
Expand Down Expand Up @@ -124,12 +115,12 @@ public function createExample(Request $request, EntityManagerInterface $em): Res

return $this->render('ast/create.html.twig', [
'form' => $form->createView(),
'example' => $createAstRunRequest,
'astRun' => $createAstRunRequest,
]);
}

#[Route('/ast/{astRunIdHash}/{activeNodeId}', name: 'app_ast_display', defaults: ['activeNodeId' => null], methods: ['GET'])]
public function displayExample(EntityManagerInterface $em, string $astRunIdHash, int|null $activeNodeId = null): Response
public function displayExample(EntityManagerInterface $em, string $astRunIdHash, ?int $activeNodeId = null): Response
{
/** @var AstRun|null $astRun */
$astRun = $em->find(AstRun::class, $this->hashids->decode($astRunIdHash)[0]);
Expand Down Expand Up @@ -161,6 +152,7 @@ public function displayExample(EntityManagerInterface $em, string $astRunIdHash,
'astRun' => $createAstRequest,
'clickableNodesDump' => $this->makeNodeClickable($nodes, $astRunIdHash, $activeNodeId),
'simpleNodeDump' => $simpleNodeDump,
'targetNodeClass' => $targetNodeClass,
]);
}

Expand All @@ -177,13 +169,18 @@ private function makeNodeClickable(array $nodes, string $uuid, ?int $activeNodeI
private function resolveTargetNodeClass(Node $node): string
{
if ($node instanceof UseUse || $node instanceof AttributeGroup) {
/** @var Node $parentNode */
$parentNode = $node->getAttribute('parent');

return $parentNode::class;
}

if ($node instanceof Attribute) {
/** @var Node $attributeGroup */
$attributeGroup = $node->getAttribute('parent');
/** @var Node $stmt */
$stmt = $attributeGroup->getAttribute('parent');

return $stmt::class;
}

Expand All @@ -202,7 +199,9 @@ private function resolveTargetNodeClass(Node $node): string

// target one level up
if ($node instanceof Identifier || $node instanceof Name || $node instanceof Variable) {
/** @var Node $parentNode */
$parentNode = $node->getAttribute('parent');

return $this->resolveTargetNodeClass($parentNode);
}

Expand Down
15 changes: 15 additions & 0 deletions app/src/Entity/AstRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,19 @@ public static function hashInput(string $code): string
{
return md5($code);
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}

public function getIdHash(): ?string
{
return $this->idHash;
}

public function getInputHash(): string
{
return $this->inputHash;
}
}
55 changes: 55 additions & 0 deletions app/src/PhpParser/AttributeKey.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* MIT License
*
* Copyright (c) 2017 RectorPHP
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

declare(strict_types=1);

Expand Down
Loading

0 comments on commit a58fa17

Please sign in to comment.