Completed
Push — master ( a24508...16fd0b )
by Jakub
01:50
created

Generator::addAsset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
  /** @var string[] */
29
  protected $assets = [];
30
  
31
  public function __construct(string $source = NULL, string $output = NULL) {
32 1
    if(is_null($source)) {
33 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...
34
    }
35 1
    $this->setSource($source);
36 1
    if(is_null($output)) {
37 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...
38
    }
39 1
    FileSystem::createDir($output);
40 1
    $this->setOutput($output);
41 1
  }
42
  
43
  public function getSource(): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
44 1
    return $this->source;
45
  }
46
  
47
  public function setSource(string $source) {
48 1
    if(is_string($source) AND is_dir($source)) {
49 1
      $this->source = realpath($source);
50
    }
51 1
  }
52
  
53
  public function getOutput(): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
54 1
    return $this->output;
55
  }
56
  
57
  public function setOutput(string $output) {
58 1
    if(is_string($output)) {
59 1
      $this->output = realpath($output);
60
    }
61 1
  }
62
  
63
  protected function getMeta(string $filename): array {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
64 1
    $resolver = new OptionsResolver;
65 1
    $resolver->setDefaults([
66 1
      "title" => "",
67
      "styles" => [],
68
      "scripts" => [],
69
    ]);
70 1
    $isArrayOfStrings = function(array $value) {
71 1
      foreach($value as $item) {
72 1
        if(!is_string($item)) {
73
          return false;
74
        }
75
      }
76 1
      return true;
77
    };
78 1
    $resolver->setAllowedTypes("title", "string");
79 1
    $resolver->setAllowedTypes("styles", "array");
80 1
    $resolver->setAllowedValues("styles", $isArrayOfStrings);
81 1
    $resolver->setAllowedTypes("scripts", "array");
82 1
    $resolver->setAllowedValues("scripts", $isArrayOfStrings);
83 1
    $metaFilename = str_replace(".md", ".neon", $filename);
84 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...
85 1
    if(file_exists($metaFilename)) {
86 1
      $meta = Neon::decode(file_get_contents($metaFilename));
87
    }
88 1
    return $resolver->resolve($meta);
89
  }
90
  
91
  protected function addAsset(string $asset): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
92 1
    if(!in_array($asset, $this->assets)) {
93 1
      $this->assets[] = realpath($asset);
94
    }
95 1
  }
96
  
97
  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...
98 1
    foreach($meta["styles"] as $index => $style) {
99 1
      if(!file_exists("$basePath/$style")) {
100 1
        unset($meta["styles"][$index]);
101 1
        continue;
102
      }
103
    }
104 1
    foreach($meta["scripts"] as $index => $script) {
105 1
      if(!file_exists("$basePath/$script")) {
106 1
        unset($meta["scripts"][$index]);
107 1
        continue;
108
      }
109
    }
110 1
    if(!count($meta["styles"])) {
111 1
      unset($meta["styles"]);
112 1
      $html = str_replace("
113 1
  %%styles%%", "", $html);
114
    }
115 1
    if(!count($meta["scripts"])) {
116 1
      unset($meta["scripts"]);
117 1
      $html = str_replace("
118 1
  %%scripts%%", "", $html);
119
    }
120 1
    if(!isset($meta["styles"]) AND !isset($meta["scripts"])) {
121 1
      return;
122
    }
123 1 View Code Duplication
    if(isset($meta["styles"])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124 1
      foreach($meta["styles"] as $index => $style) {
125 1
        $this->addAsset("$basePath/$style");
126 1
        $meta["styles"][$index] = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style\">";
127
      }
128 1
      $meta["styles"] = implode("\n  ", $meta["styles"]);
129
    }
130 1 View Code Duplication
    if(isset($meta["scripts"])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 1
      foreach($meta["scripts"] as $index => $script) {
0 ignored issues
show
Bug introduced by
The expression $meta['scripts'] of type string is not traversable.
Loading history...
132 1
        $this->addAsset("$basePath/$script");
133 1
        $meta["scripts"][$index] = "<script type=\"text/javascript\" src=\"$script\"></script>";
134
      }
135 1
      $meta["scripts"] = implode("\n  ", $meta["scripts"]);
136
    }
137 1
  }
138
  
139
  protected function createHtml(string $filename): string {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
140 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...
141 1
    $parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true;
142 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...
143 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...
144 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...
145 1
    if(substr($source, -1) === PHP_EOL) {
146 1
      $source = substr($source, 0, -1);
147
    }
148 1
    if(strlen($meta["title"]) === 0) {
149 1
      unset($meta["title"]);
150 1
      $html = str_replace("
151 1
  <title>%%title%%</title>", "", $html);
152
    }
153 1
    $this->processAssets($meta, $html, dirname($filename));
154 1
    $meta["source"] = $source;
155 1
    foreach($meta as $key => $value) {
156 1
      $html = str_replace("%%$key%%", $value, $html);
157
    }
158 1
    return $html;
159
  }
160
  
161
  /**
162
   * Generate the site
163
   */
164
  public function generate(): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
165 1
    FileSystem::delete($this->output);
166 1
    $files = Finder::findFiles("*.md")
167 1
      ->exclude("README.md")
168 1
      ->from($this->source)
169 1
      ->exclude("vendor", ".git", "tests");
170
    /** @var \SplFileInfo $file */
171 1
    foreach($files as $file) {
172 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...
173 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...
174 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...
175 1
      $filename = "$this->output$path/{$file->getBasename(".md")}.html";
176 1
      FileSystem::write($filename, $html);
177 1
      echo "Created $path/{$file->getBasename(".md")}.html\n";
178
    }
179 1
    foreach($this->assets as $asset) {
180 1
      $path = str_replace($this->source, "", $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...
181 1
      $target = "$this->output$path";
182 1
      FileSystem::copy($asset, $target);
183 1
      echo "Copied $path";
184
    }
185 1
  }
186
}
187
?>