Skip to content

Commit

Permalink
Merge pull request #5 from DeGraciaMathieu/feat/add-render-module
Browse files Browse the repository at this point in the history
feat : added render module
  • Loading branch information
DeGraciaMathieu authored Jul 12, 2023
2 parents 46f86ad + 0cedcb0 commit 7e9db14
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ php smelly-code-detector inspect-method {path}
| --protected | Show only protected methods.|
| --without-constructor | Hide constructors.|
| --sort-by=smell | Sort order (smell, loc, arg, ccl), default smell.|
| --json | Render metrics in JSON|


## Examples
Expand Down Expand Up @@ -151,7 +152,7 @@ php smelly-code-detector inspect-class {path}
| --protected | Show only protected methods.|
| --without-constructor | Hide constructors.|
| --sort-by=smell | Sort order (count, smell, avg), default smell.|

| --json | Render metrics in JSON|

## Examples

Expand Down
28 changes: 17 additions & 11 deletions app/Commands/InspectClassCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
};

use App\Modules\Render\ {
RendererFactory,
RenderService,
Pipes\SortRows,
Pipes\CutRows,
Dtos\Option,
};

class InspectClassCommand extends Command
Expand All @@ -38,7 +40,9 @@ class InspectClassCommand extends Command
{--private : Show only private methods.}
{--protected : Show only protected methods.}
{--without-constructor : Hide constructors.}
{--sort-by=smell : Sort order (count, smell, avg).}';
{--sort-by=smell : Sort order (count, smell, avg).}
{--json : Render metrics in JSON.}
';

/**
* The description of the command.
Expand All @@ -49,7 +53,7 @@ class InspectClassCommand extends Command

public function handle(): void
{
$this->info('❀ PHP Smelly Code Detector ❀');
$this->hello();

$files = $this->getAllFiles();

Expand All @@ -59,10 +63,7 @@ public function handle(): void

$rows = $this->prepareMetricsToBeDisplayed($metrics);

$this->display([
'displayableRows' => $rows,
'numberOfRows' => count($metrics),
]);
$this->display($rows, $metrics);
}

private function analysisMetricsBag(array $analysis): array
Expand Down Expand Up @@ -114,11 +115,16 @@ private function prepareMetricsToBeDisplayed(array $metrics): array
);
}

private function makeHtml(array $attributes): ViewContract
private function display(array $rows, array $metrics): void
{
return View::make('inspect-class', [
'displayableRows' => $attributes['displayableRows'],
'numberOfRows' => $attributes['numberOfRows'],
]);
$renderer = $this->makeRendererInstance();

$renderer->display(
view: 'inspect-class',
attributes: [
'displayableRows' => $rows,
'numberOfRows' => count($metrics),
],
);
}
}
26 changes: 15 additions & 11 deletions app/Commands/InspectMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class InspectMethodCommand extends Command
{--private : Show only private methods.}
{--protected : Show only protected methods.}
{--without-constructor : Hide constructors.}
{--sort-by=smell : Sort order (smell, loc, arg, ccl).}';
{--sort-by=smell : Sort order (smell, loc, arg, ccl).}
{--json : Render metrics in JSON.}
';

/**
* The description of the command.
Expand All @@ -53,7 +55,7 @@ class InspectMethodCommand extends Command
*/
public function handle(): void
{
$this->info('❀ PHP Smelly Code Detector ❀');
$this->hello();

$files = $this->getAllFiles();

Expand All @@ -63,10 +65,7 @@ public function handle(): void

$rows = $this->prepareMetricsToBeDisplayed($metrics);

$this->display([
'displayableRows' => $rows,
'numberOfRows' => count($metrics),
]);
$this->display($rows, $metrics);
}

private function analysisMetricsBag(array $analysis): array
Expand Down Expand Up @@ -119,11 +118,16 @@ private function prepareMetricsToBeDisplayed(array $metrics): array
);
}

private function makeHtml(array $attributes): ViewContract
private function display(array $rows, array $metrics): void
{
return View::make('inspect-method', [
'displayableRows' => $attributes['displayableRows'],
'numberOfRows' => $attributes['numberOfRows'],
]);
$renderer = $this->makeRendererInstance();

$renderer->display(
view: 'inspect-method',
attributes: [
'displayableRows' => $rows,
'numberOfRows' => count($metrics),
],
);
}
}
28 changes: 17 additions & 11 deletions app/Commands/Traits/HtmlRenderingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@

namespace App\Commands\Traits;

use Termwind\HtmlRenderer;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Contracts\View\View as ViewContract;
use App\Modules\Render\RendererFactory;
use App\Modules\Render\Dtos\Option;
use App\Modules\Render\Contracts\Renderer;

trait HtmlRenderingTrait
{
abstract protected function makeHtml(array $attributes): ViewContract;
abstract private function display(array $rows, array $metrics): void;

protected function display(array $attributes): void
protected function hello()
{
$html = $this->makeHtml($attributes);
/**
* In the case of rendering in JSON,
* the prompt should not be polluted
*/
$mustSayHello = ! $this->option('json');

$this->renderHtml($html);
if ($mustSayHello) {
$this->info('❀ PHP Smelly Code Detector ❀');
}
}

private function renderHtml($html): void
protected function makeRendererInstance(): Renderer
{
$htmlRenderer = new HtmlRenderer();

$htmlRenderer->render($html, OutputInterface::OUTPUT_NORMAL);
return app(RendererFactory::class)->from(
Option::fromCommand($this->options()),
);
}
}
8 changes: 8 additions & 0 deletions app/Modules/Render/Contracts/Renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Modules\Render\Contracts;

interface Renderer
{
public function display(string $view, array $attributes): void;
}
19 changes: 19 additions & 0 deletions app/Modules/Render/Dtos/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Modules\Render\Dtos;

use App\Modules\Render\Enums\RenderType;

class Option
{
public function __construct(
public RenderType $type,
) {}

public static function fromCommand(array $attributes): Option
{
$type = $attributes['json'] ? RenderType::Json : RenderType::View;

return new self($type);
}
}
9 changes: 9 additions & 0 deletions app/Modules/Render/Enums/RenderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Modules\Render\Enums;

enum RenderType: string
{
case Json = 'json';
case View = 'view';
}
23 changes: 23 additions & 0 deletions app/Modules/Render/RendererFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Modules\Render;

use App\Modules\Render\Dtos\Option;
use App\Modules\Render\Contracts\Renderer;
use App\Modules\Render\Renderers\JsonRenderer;
use App\Modules\Render\Renderers\ViewRenderer;

class RendererFactory
{
private array $renderers = [
'json' => JsonRenderer::class,
'view' => ViewRenderer::class,
];

public function from(Option $option): Renderer
{
$renderer = $this->renderers[$option->type->value];

return app($renderer);
}
}
13 changes: 13 additions & 0 deletions app/Modules/Render/Renderers/JsonRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Modules\Render\Renderers;

use App\Modules\Render\Contracts\Renderer;

class JsonRenderer implements Renderer
{
public function display(string $view, array $attributes): void
{
echo json_encode($attributes);
}
}
34 changes: 34 additions & 0 deletions app/Modules/Render/Renderers/ViewRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Modules\Render\Renderers;

use App\Modules\Render\Contracts\Renderer;
use Termwind\HtmlRenderer;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\View\Factory as ViewFactory;

class ViewRenderer implements Renderer
{
public function __construct(
private ViewFactory $view,
private HtmlRenderer $htmlRenderer,
) {}

public function display(string $view, array $attributes): void
{
$html = $this->makeHtml($view, $attributes);

$this->renderHtml($html);
}

protected function makeHtml(string $view, array $attributes): ViewContract
{
return $this->view->make($view, $attributes);
}

private function renderHtml(ViewContract $html): void
{
$this->htmlRenderer->render($html, OutputInterface::OUTPUT_NORMAL);
}
}
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ parameters:
level: 5
ignoreErrors:
- '#Match expression does not handle remaining value: mixed#'
- '#Property App\\Modules\\Analyzer\\Visitors\\ClassMethodVisitor::\$metrics is never read, only written\.#'
- '#Property App\\Modules\\Analyzer\\Visitors\\ClassMethodVisitor::\$metrics is never read, only written\.#'
- '#Parameter \#1 \$html of method Termwind\\HtmlRenderer::render\(\) expects string, Illuminate\\Contracts\\View\\View given\.#'
44 changes: 44 additions & 0 deletions tests/Unit/Render/RendererFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Unit\Render;

use Tests\TestCase;
use App\Modules\Render\RendererFactory;
use App\Modules\Render\Dtos\Option;
use App\Modules\Render\Renderers\JsonRenderer;
use App\Modules\Render\Renderers\ViewRenderer;

class RendererFactoryTest extends TestCase
{
/**
* @test
*/
public function it_able_to_instantiate_json_renderer(): void
{
$factory = new RendererFactory();

$renderer = $factory->from(
Option::fromCommand(attributes: [
'json' => true,
]),
);

$this->assertInstanceOf(JsonRenderer::class, $renderer);
}

/**
* @test
*/
public function it_able_to_instantiate_view_renderer(): void
{
$factory = new RendererFactory();

$renderer = $factory->from(
Option::fromCommand(attributes: [
'json' => false,
]),
);

$this->assertInstanceOf(ViewRenderer::class, $renderer);
}
}

0 comments on commit 7e9db14

Please sign in to comment.