Completed
Push — master ( d6283a...7e7429 )
by Jakub
01:47
created

Generator   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 158
Duplicated Lines 8.86 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 98.97%

Importance

Changes 0
Metric Value
dl 14
loc 158
ccs 96
cts 97
cp 0.9897
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSource() 0 3 1
A setSource() 0 5 2
A getOutput() 0 3 1
A setOutput() 0 3 1
B getMeta() 0 27 4
A addAsset() 0 5 2
F processAssets() 14 41 13
A createHtml() 0 21 4
A generate() 0 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\SiteGenerator;
5
6
use cebe\markdown\GithubMarkdown,
7
    Nette\Utils\Finder,
8
    Nette\Neon\Neon,
9
    Nette\Utils\FileSystem,
10
    Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * Generator
14
 *
15
 * @author Jakub Konečný
16
 * @property string $source
17
 * @property string $output
18
 */
19 1
class Generator {
20 1
  use \Nette\SmartObject;
21
  
22
  /** @var string */
23
  protected $source;
24
  /** @var string */
25
  protected $output;
26
  /** @var string[] */
27
  protected $assets = [];
28
  
29
  public function __construct(string $source, string $output) {
30 1
    $this->setSource($source);
31 1
    FileSystem::createDir($output);
32 1
    $this->setOutput($output);
33 1
  }
34
  
35
  public function getSource(): string {
36 1
    return $this->source;
37
  }
38
  
39
  public function setSource(string $source) {
40 1
    if(is_dir($source)) {
41 1
      $this->source = realpath($source);
42
    }
43 1
  }
44
  
45
  public function getOutput(): string {
46 1
    return $this->output;
47
  }
48
  
49
  public function setOutput(string $output) {
50 1
    $this->output = realpath($output);
51 1
  }
52
  
53
  protected function getMeta(string $filename): array {
54 1
    $resolver = new OptionsResolver;
55 1
    $resolver->setDefaults([
56 1
      "title" => "",
57
      "styles" => [],
58
      "scripts" => [],
59
    ]);
60 1
    $isArrayOfStrings = function(array $value) {
61 1
      foreach($value as $item) {
62 1
        if(!is_string($item)) {
63
          return false;
64
        }
65
      }
66 1
      return true;
67
    };
68 1
    $resolver->setAllowedTypes("title", "string");
69 1
    $resolver->setAllowedTypes("styles", "array");
70 1
    $resolver->setAllowedValues("styles", $isArrayOfStrings);
71 1
    $resolver->setAllowedTypes("scripts", "array");
72 1
    $resolver->setAllowedValues("scripts", $isArrayOfStrings);
73 1
    $metaFilename = str_replace(".md", ".neon", $filename);
74 1
    $meta = [];
75 1
    if(file_exists($metaFilename)) {
76 1
      $meta = Neon::decode(file_get_contents($metaFilename));
77
    }
78 1
    return $resolver->resolve($meta);
79
  }
80
  
81
  protected function addAsset(string $asset): void {
82 1
    if(!in_array($asset, $this->assets)) {
83 1
      $this->assets[] = realpath($asset);
84
    }
85 1
  }
86
  
87
  protected function processAssets(array &$meta, string &$html, string $basePath): void {
0 ignored issues
show
introduced by
Function's cyclomatic complexity (12) exceeds 10; consider refactoring the function
Loading history...
introduced by
Line exceeds 80 characters; contains 89 characters
Loading history...
88 1
    foreach($meta["styles"] as $index => $style) {
89 1
      if(!file_exists("$basePath/$style")) {
90 1
        unset($meta["styles"][$index]);
91 1
        continue;
92
      }
93
    }
94 1
    foreach($meta["scripts"] as $index => $script) {
95 1
      if(!file_exists("$basePath/$script")) {
96 1
        unset($meta["scripts"][$index]);
97 1
        continue;
98
      }
99
    }
100 1
    if(!count($meta["styles"])) {
101 1
      unset($meta["styles"]);
102 1
      $html = str_replace("
103 1
  %%styles%%", "", $html);
104
    }
105 1
    if(!count($meta["scripts"])) {
106 1
      unset($meta["scripts"]);
107 1
      $html = str_replace("
108 1
  %%scripts%%", "", $html);
109
    }
110 1
    if(!isset($meta["styles"]) AND !isset($meta["scripts"])) {
111 1
      return;
112
    }
113 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...
114 1
      foreach($meta["styles"] as $index => $style) {
115 1
        $this->addAsset("$basePath/$style");
116 1
        $meta["styles"][$index] = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style\">";
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 96 characters
Loading history...
117
      }
118 1
      $meta["styles"] = implode("\n  ", $meta["styles"]);
119
    }
120 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...
121 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...
122 1
        $this->addAsset("$basePath/$script");
123 1
        $meta["scripts"][$index] = "<script type=\"text/javascript\" src=\"$script\"></script>";
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 96 characters
Loading history...
124
      }
125 1
      $meta["scripts"] = implode("\n  ", $meta["scripts"]);
126
    }
127 1
  }
128
  
129
  protected function createHtml(string $filename): string {
130 1
    $parser = new GithubMarkdown;
131 1
    $parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true;
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 83 characters
Loading history...
132 1
    $source = $parser->parse(file_get_contents($filename));
133 1
    $meta = $this->getMeta($filename);
134 1
    $html = file_get_contents(__DIR__ . "/template.html");
135 1
    if(substr($source, -1) === PHP_EOL) {
136 1
      $source = substr($source, 0, -1);
137
    }
138 1
    if(strlen($meta["title"]) === 0) {
139 1
      unset($meta["title"]);
140 1
      $html = str_replace("
141 1
  <title>%%title%%</title>", "", $html);
142
    }
143 1
    $this->processAssets($meta, $html, dirname($filename));
144 1
    $meta["source"] = $source;
145 1
    foreach($meta as $key => $value) {
146 1
      $html = str_replace("%%$key%%", $value, $html);
147
    }
148 1
    return $html;
149
  }
150
  
151
  /**
152
   * Generate the site
153
   */
154
  public function generate(): void {
155 1
    FileSystem::delete($this->output);
156 1
    $files = Finder::findFiles("*.md")
157 1
      ->exclude("README.md")
158 1
      ->from($this->source)
159 1
      ->exclude("vendor", ".git", "tests");
160
    /** @var \SplFileInfo $file */
161 1
    foreach($files as $file) {
162 1
      $path = dirname($file->getRealPath());
163 1
      $path = str_replace($this->source, "", $path);
164 1
      $html = $this->createHtml($file->getRealPath());
165 1
      $filename = "$this->output$path/{$file->getBasename(".md")}.html";
166 1
      FileSystem::write($filename, $html);
167 1
      echo "Created $path/{$file->getBasename(".md")}.html\n";
168
    }
169 1
    foreach($this->assets as $asset) {
170 1
      $path = str_replace($this->source, "", $asset);
171 1
      $target = "$this->output$path";
172 1
      FileSystem::copy($asset, $target);
173 1
      echo "Copied $path";
174
    }
175 1
  }
176
}
177
?>