Completed
Push — master ( ef2528...a24508 )
by Jakub
01:57
created

Generator::processAssets()   F

Complexity

Conditions 12
Paths 324

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 30
nc 324
nop 3
dl 0
loc 41
ccs 30
cts 30
cp 1
crap 12
rs 3.7956
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Nexendrie\SiteGenerator;
5
6 1
require_once(__DIR__ . "/functions.php");
7
8
use cebe\markdown\GithubMarkdown,
9
    Nette\Utils\Finder,
10
    Nette\Neon\Neon,
11
    Nette\Utils\FileSystem,
12
    Symfony\Component\OptionsResolver\OptionsResolver;
13
14
/**
15
 * Generator
16
 *
17
 * @author Jakub Konečný
18
 * @property string $source
19
 * @property string $output
20
 */
21 1
class Generator {
22 1
  use \Nette\SmartObject;
23
  
24
  /** @var string */
25
  protected $source;
26
  /** @var string */
27
  protected $output;
28
  
29
  public function __construct(string $source = NULL, string $output = NULL) {
30 1
    if(is_null($source)) {
31 1
      $source = findVendorDirectory() . "/../";
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $source. This often makes code more readable.
Loading history...
32
    }
33 1
    $this->setSource($source);
34 1
    if(is_null($output)) {
35 1
      $output = findVendorDirectory() . "/../public/";
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $output. This often makes code more readable.
Loading history...
36
    }
37 1
    FileSystem::createDir($output);
38 1
    $this->setOutput($output);
39 1
  }
40
  
41
  public function getSource(): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
42 1
    return $this->source;
43
  }
44
  
45
  public function setSource(string $source) {
46 1
    if(is_string($source) AND is_dir($source)) {
47 1
      $this->source = realpath($source);
48
    }
49 1
  }
50
  
51
  public function getOutput(): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
52 1
    return $this->output;
53
  }
54
  
55
  public function setOutput(string $output) {
56 1
    if(is_string($output)) {
57 1
      $this->output = realpath($output);
58
    }
59 1
  }
60
  
61
  protected function getMeta(string $filename): array {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
62 1
    $resolver = new OptionsResolver;
63 1
    $resolver->setDefaults([
64 1
      "title" => "",
65
      "styles" => [],
66
      "scripts" => [],
67
    ]);
68 1
    $isArrayOfStrings = function(array $value) {
69 1
      foreach($value as $item) {
70 1
        if(!is_string($item)) {
71
          return false;
72
        }
73
      }
74 1
      return true;
75
    };
76 1
    $resolver->setAllowedTypes("title", "string");
77 1
    $resolver->setAllowedTypes("styles", "array");
78 1
    $resolver->setAllowedValues("styles", $isArrayOfStrings);
79 1
    $resolver->setAllowedTypes("scripts", "array");
80 1
    $resolver->setAllowedValues("scripts", $isArrayOfStrings);
81 1
    $metaFilename = str_replace(".md", ".neon", $filename);
82 1
    $meta = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
83 1
    if(file_exists($metaFilename)) {
84 1
      $meta = Neon::decode(file_get_contents($metaFilename));
85
    }
86 1
    return $resolver->resolve($meta);
87
  }
88
  
89
  protected function processAssets(array &$meta, string &$html, string $basePath): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
90 1
    foreach($meta["styles"] as $index => $style) {
91 1
      if(!file_exists("$basePath/$style")) {
92 1
        unset($meta["styles"][$index]);
93 1
        continue;
94
      }
95
    }
96 1
    foreach($meta["scripts"] as $index => $script) {
97 1
      if(!file_exists("$basePath/$script")) {
98 1
        unset($meta["scripts"][$index]);
99 1
        continue;
100
      }
101
    }
102 1
    if(!count($meta["styles"])) {
103 1
      $html = str_replace("
104 1
  %%styles%%", "", $html);
105
    }
106 1
    if(!count($meta["scripts"])) {
107 1
      $html = str_replace("
108 1
  %%scripts%%", "", $html);
109
    }
110 1
    if(!count($meta["styles"]) AND !count($meta["scripts"])) {
111 1
      unset($meta["styles"]);
112 1
      unset($meta["scripts"]);
113 1
      return;
114
    }
115 1
    $assets = array_merge($meta["styles"], $meta["scripts"]);
116 1
    foreach($assets as $asset) {
117 1
      $path = str_replace($this->source, "", realpath($asset));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
118 1
      $target = "$this->output$path/$asset";
119 1
      FileSystem::copy("$basePath/$asset", $target);
120
    }
121 1
    foreach($meta["styles"] as $index => $style) {
122 1
      $meta["styles"][$index] = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style\">";
123
    }
124 1
    foreach($meta["scripts"] as $index => $script) {
125 1
      $meta["scripts"][$index] = "<script type=\"text/javascript\" src=\"$script\"></script>";
126
    }
127 1
    $meta["styles"] = implode("\n  ", $meta["styles"]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128 1
    $meta["scripts"] = implode("\n  ", $meta["scripts"]);
129 1
  }
130
  
131
  protected function createHtml(string $filename): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
132 1
    $parser = new GithubMarkdown;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
133 1
    $parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true;
134 1
    $source = $parser->parse(file_get_contents($filename));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135 1
    $meta = $this->getMeta($filename);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
136 1
    $html = file_get_contents(__DIR__ . "/template.html");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
137 1
    if(substr($source, -1) === PHP_EOL) {
138 1
      $source = substr($source, 0, -1);
139
    }
140 1
    if(strlen($meta["title"]) === 0) {
141 1
      unset($meta["title"]);
142 1
      $html = str_replace("
143 1
  <title>%%title%%</title>", "", $html);
144
    }
145 1
    $this->processAssets($meta, $html, dirname($filename));
146 1
    $meta["source"] = $source;
147 1
    foreach($meta as $key => $value) {
148 1
      $html = str_replace("%%$key%%", $value, $html);
149
    }
150 1
    return $html;
151
  }
152
  
153
  /**
154
   * Generate the site
155
   */
156
  public function generate(): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
157 1
    FileSystem::delete($this->output);
158 1
    $files = Finder::findFiles("*.md")
159 1
      ->exclude("README.md")
160 1
      ->from($this->source)
161 1
      ->exclude("vendor", ".git", "tests");
162
    /** @var \SplFileInfo $file */
163 1
    foreach($files as $file) {
164 1
      $path = dirname($file->getRealPath());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
165 1
      $path = str_replace($this->source, "", $path);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
166 1
      $html = $this->createHtml($file->getRealPath());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
167 1
      $filename = "$this->output$path/{$file->getBasename(".md")}.html";
168 1
      FileSystem::write($filename, $html);
169 1
      echo "Created $path/{$file->getBasename(".md")}.html\n";
170
    }
171 1
  }
172
}
173
?>