FilesTreatment   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A filesFromDirectory() 0 13 6
A createDocs() 0 18 5
1
<?php
2
namespace application;
3
require dirname(__DIR__) . '/create_documentation/PDFFile.php';
4
5
use create_documentation\PDFFile;
6
7
/**
8
 * Class FilesTreatment
9
 * @package application
10
 */
11
class FilesTreatment {
12
13
    /**
14
     * @var \phpDocumentor\Reflection\File\LocalFile
15
     */
16
    private $files = array();
17
18
    /**
19
     * FilesTreatment constructor.
20
     * @param string $fileOrDir
21
     * @throws \Exception if $fileOrDir is not file or directory.
22
     */
23
    public function __construct(string $fileOrDir) {
24
        if (is_file($fileOrDir)) {
25
            $this->files[] = new \phpDocumentor\Reflection\File\LocalFile($fileOrDir);
26
        } else if (is_dir($fileOrDir)) {
27
            $this->files = self::filesFromDirectory($fileOrDir);
28
        } else {
29
            throw new \Exception('Input should be a file or a directory.');
30
        }
31
    }
32
33
    /**
34
     * @param \Symfony\Component\Console\Output\OutputInterface $output
35
     * @param string $outputPath
36
     * @throws \Mpdf\MpdfException
37
     * @throws \phpDocumentor\Reflection\Exception
38
     */
39
    public function createDocs(\Symfony\Component\Console\Output\OutputInterface $output, string $outputPath): void {
40
        $projectFactory = \phpDocumentor\Reflection\Php\ProjectFactory::createInstance();
41
        $project = $projectFactory->create('Project to document', $this->files);
42
        foreach ($project->getFiles() as $file) {
43
            foreach ($file->getClasses() as $class) {
44
                $pdfFile = new PDFFile($outputPath . '/' . $class->getName());
45
                $output->writeln('Documenting class ' . $class->getName());
46
                $pdfFile->createFromClass($class);
47
            }
48
            foreach ($file->getInterfaces() as $interface) {
49
                $pdfFile = new PDFFile($outputPath . '/' . $interface->getName());
50
                $output->writeln('Documenting interface ' . $interface->getName());
51
                $pdfFile->createFromInterface($interface);
52
            }
53
            foreach ($file->getTraits() as $trait) {
54
                $pdfFile = new PDFFile($outputPath . '/' . $trait->getName());
55
                $output->writeln('Documenting trait ' . $trait->getName());
56
                $pdfFile->createFromTrait($trait);
57
            }
58
        }
59
    }
60
61
    /**
62
     * @param string $directory
63
     * @param string[] $results
64
     * @return \phpDocumentor\Reflection\File\LocalFile[]
65
     */
66
    private static function filesFromDirectory(string $directory, array &$results = array()): array {
67
        $files = scandir($directory);
68
        foreach($files as $value) {
69
            $path = realpath($directory . DIRECTORY_SEPARATOR . $value);
70
            if(!is_dir($path)) {
71
                if (pathinfo($path, PATHINFO_EXTENSION) === 'php') {
72
                    $results[] = new \phpDocumentor\Reflection\File\LocalFile($path);
73
                }
74
            } else if($value != '.' && $value != '..') {
75
                self::filesFromDirectory($path, $results);
76
            }
77
        }
78
        return $results;
79
    }
80
}