Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
fix compiling warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
abdumu committed Aug 3, 2018
1 parent 19b55ea commit 1e42b3e
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/Analyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\View\Factory;
use Illuminate\View\Compilers\BladeCompiler;


class Analyze
{
/** @var \Illuminate\View\Compilers\BladeCompiler */
Expand All @@ -18,6 +19,8 @@ class Analyze
protected $finder;

protected $code;

/** @var \Illuminate\Support\Collection */
protected $directives;

protected $viewInfo;
Expand Down Expand Up @@ -58,7 +61,10 @@ public function analyze($viewName)
PREG_SET_ORDER
);

$this->directives = array_map('strtolower', array_pluck($m, 1));
$this->directives = Collection::wrap($m)
->map(function ($v) {
return [$v[0], strtolower($v[1])];
});

$this->warnings = Collection::make();

Expand All @@ -75,7 +81,10 @@ protected function calculateNestingLevel()
$currentNestingLevel = 0;
$nestedDirectives = Collection::make();

foreach ($this->directives as $directive) {
foreach ($this->directives as $item) {

[$code, $directive] = $item;

if (Str::startsWith($directive, ['end', 'stop'])) {
if ($currentNestingLevel > 0) {
$currentNestingLevel--;
Expand All @@ -87,7 +96,7 @@ protected function calculateNestingLevel()
} else {
$nestedDirectives->push([$directive, $currentNestingLevel]);

if ($this->isBlockDirective($directive)) {
if ($this->isBlockDirective($code)) {
$currentNestingLevel++;
}
}
Expand All @@ -98,10 +107,10 @@ protected function calculateNestingLevel()

protected function fetchDirectivesInfo()
{
$this->directivesInfo = Collection::wrap(array_count_values($this->directives));
$this->directivesInfo = Collection::wrap(array_count_values($this->directives->pluck(1)->toArray()));

$this->directivesInfo = $this->directivesInfo->map(function ($v, $k) {
$customDirective = ! method_exists(BladeCompiler::class, 'compile'.$k);
$customDirective = !method_exists(BladeCompiler::class, 'compile' . $k);

return [$k, $v, $customDirective ? 'custom' : 'built-in'];
});
Expand All @@ -116,7 +125,7 @@ protected function fetchViewInfo()

$linesNumber = substr_count($this->code, "\n");

$this->viewInfo->push(['Lines', $linesNumber]);
$this->viewInfo->push(['Lines', $linesNumber]);

if ($linesNumber > 300) {
$this->warnings->push(['lines > 300', sprintf('View has %d lines, it\'s a good idea to seperate & @include codes.', $linesNumber)]);
Expand All @@ -127,15 +136,15 @@ protected function fetchViewInfo()
$this->viewInfo->push(['Longest Line (chars)', max($lines)]);

//directives number
$directivesNumber = Collection::wrap($this->directives)
->filter(function ($item) {
return ! Str::startsWith($item, ['end', 'stop', 'else']);
})->count();
$directivesNumber = $this->directives
->filter(function ($item) {
return !Str::startsWith($item[0], ['end', 'stop', 'else']);
})->count();

$this->viewInfo->push(['Directives', $directivesNumber]);

//html & css
if (class_exists(\DOMDocument::class) && ! empty($this->code)) {
if (class_exists(\DOMDocument::class) && !empty($this->code)) {
$dom = new \DOMDocument();
$dom->loadHTML($this->code, LIBXML_NOERROR | LIBXML_NONET | LIBXML_NOWARNING);
$allElements = $dom->getElementsByTagName('*');
Expand All @@ -147,7 +156,7 @@ protected function fetchViewInfo()
protected function detectWarnings()
{
//php directive?
if (in_array('php', $this->directives)) {
if (strpos($this->code, '@php') !== false) {
$this->warnings->push(['@php', 'Is not recommended to use php codes directly in your view.']);
}

Expand Down Expand Up @@ -188,9 +197,13 @@ protected function detectWarnings()
}
}

protected function isBlockDirective($name)
protected function isBlockDirective($code)
{
return Str::contains($this->compiler->compileString('@'.$name), [' if', ' for', ' foreach', ' else', '->startSection', '->startComponent', ' while']);
try {
return Str::contains($this->compiler->compileString($code), [' if', ' for', ' foreach', ' else', '->startSection', '->startComponent', ' while']);
} catch (\Exception $e) {
return false;
}
}

/**
Expand Down

0 comments on commit 1e42b3e

Please sign in to comment.