Skip to content

Commit

Permalink
[Docs] Code count (#41)
Browse files Browse the repository at this point in the history
* Add count of code
  • Loading branch information
ArtARTs36 authored Dec 25, 2021
1 parent 58ad360 commit e8bc297
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 12 deletions.
44 changes: 44 additions & 0 deletions docs/DocBuilder/CodeCountsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ArtARTs36\GitHandler\DocBuilder;

use ArtARTs36\Str\Facade\Str;

class CodeCountsBuilder
{
private $statist;

private $paths;

/**
* @param array<string, string> $paths
*/
public function __construct(
FolderStatist $statist,
array $paths
) {
$this->statist = $statist;
$this->paths = $paths;
}

public function build(): string
{
$content = Markdown::tableHeader([
'Type',
'Files\' count',
'Code lines\' count',
]) . "\n";

foreach ($this->paths as $type => $path) {
$statistic = $this->statist->calculate($path);

$content .= Markdown::tableLine([
Str::upFirstSymbol($type),
$statistic->codeFiles,
$statistic->codeLines,
]) . "\n";
}

return $content;
}
}
5 changes: 4 additions & 1 deletion docs/DocBuilder/DevelopmentCommandsTableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function build(): string
$view = '';

foreach ($this->buildMap() as $command => $description) {
$view .= "| ". $command . ' | ' . $description . "\n";
$view .= Markdown::tableLine([
$command,
$description
]) . "\n";
}

return rtrim($view);
Expand Down
2 changes: 1 addition & 1 deletion docs/DocBuilder/FeatureBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function build(\ReflectionMethod $method, string $factoryMethodName): str
return $this->stubs->load('page_git_command_feature_content.md')->render([
'featureName' => $docBlock->getSummary(),
'realGitCommands' => implode("\n\n", array_map(function (string $command) {
return "`" . $command . "`";
return Markdown::tag($command);
}, $gitCommands)),
'factoryMethodName' => $factoryMethodName,
'featureMethodName' => $method->getShortName(),
Expand Down
52 changes: 52 additions & 0 deletions docs/DocBuilder/FolderStatist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace ArtARTs36\GitHandler\DocBuilder;

use ArtARTs36\FileSystem\Contracts\FileSystem;
use ArtARTs36\Str\Facade\Str;

class FolderStatist
{
private $files;

public function __construct(FileSystem $files)
{
$this->files = $files;
}

public function calculate(string $path): FolderStatistic
{
$codeLines = 0;
$codeFiles = 0;

foreach ($this->getFromDirectory($path) as $file) {
if ($this->isCode($file)) {
$codeFiles++;

$codeLines += count(file($file));
}
}

return new FolderStatistic($path, $codeFiles, $codeLines);
}

private function isCode(string $file): bool
{
return Str::endsWith($file, '.php');
}

private function getFromDirectory(string $path): array
{
$files = [];

foreach ($this->files->getFromDirectory($path) as $file) {
if (is_dir($file)) {
array_push($files, ...$this->getFromDirectory($file));
} else {
$files[] = $file;
}
}

return $files;
}
}
19 changes: 19 additions & 0 deletions docs/DocBuilder/FolderStatistic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ArtARTs36\GitHandler\DocBuilder;

class FolderStatistic
{
public $path;

public $codeFiles;

public $codeLines;

public function __construct(string $path, int $codeFiles, int $codeLines)
{
$this->path = $path;
$this->codeFiles = $codeFiles;
$this->codeLines = $codeLines;
}
}
11 changes: 9 additions & 2 deletions docs/DocBuilder/HomePageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ class HomePageBuilder

protected $devCommands;

public function __construct(StubLoader $stubs, DevelopmentCommandsTableBuilder $devCommands)
{
private $codeCounts;

public function __construct(
StubLoader $stubs,
DevelopmentCommandsTableBuilder $devCommands,
CodeCountsBuilder $codeCounts
) {
$this->stubs = $stubs;
$this->devCommands = $devCommands;
$this->codeCounts = $codeCounts;
}

/**
Expand All @@ -24,6 +30,7 @@ public function build(array $pages): string
return '* '. Markdown::link($page->title, 'docs/'. $page->file);
}, $pages)),
'dev-commands' => $this->devCommands->build(),
'code-counts' => $this->codeCounts->build(),
]);
}
}
16 changes: 16 additions & 0 deletions docs/DocBuilder/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,20 @@ public static function link(string $title, string $path): string
{
return "[$title]($path)";
}

public static function tag(string $text): string
{
return "`$text`";
}

public static function tableLine(array $items): string
{
return '| ' . implode(' | ', $items) . ' | ';
}

public static function tableHeader(array $rows): string
{
return '| ' . implode(' | ', $rows) . ' | ' . "\n" .
'| ' . implode(' | ', array_fill(0, count($rows), '------------')) . ' | ';
}
}
11 changes: 10 additions & 1 deletion docs/DocBuilder/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

use ArtARTs36\GitHandler\Contracts\Handler\GitHandler;
use ArtARTs36\GitHandler\DocBuilder\ClassFinder;
use ArtARTs36\GitHandler\DocBuilder\CodeCountsBuilder;
use ArtARTs36\GitHandler\DocBuilder\DevelopmentCommandsTableBuilder;
use ArtARTs36\GitHandler\DocBuilder\DocBuilder;
use ArtARTs36\GitHandler\DocBuilder\DocCommandPageBuilder;
use ArtARTs36\GitHandler\DocBuilder\FolderStatist;
use ArtARTs36\GitHandler\DocBuilder\HomePageBuilder;
use ArtARTs36\GitHandler\DocBuilder\Page;
use ArtARTs36\GitHandler\DocBuilder\Project;
Expand All @@ -26,7 +28,14 @@
new DocCommandPageBuilder($stubLoader, $project)
);

$homePageBuilder = new HomePageBuilder($stubLoader, new DevelopmentCommandsTableBuilder($project));
$homePageBuilder = new HomePageBuilder(
$stubLoader,
new DevelopmentCommandsTableBuilder($project),
new CodeCountsBuilder(new FolderStatist($fileSystem), [
'Source' => __DIR__ . '/../../src',
'Tests' => __DIR__ . '/../../tests',
]),
);

$pages = $docBuilder->build();

Expand Down
4 changes: 4 additions & 0 deletions docs/DocBuilder/stubs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ Tool for work with Git in PHP
| Command | Description |
| ------------ | ------------ |
{$dev-commands}

## Count of code

{$code-counts}
22 changes: 15 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ Tool for work with Git in PHP

| Command | Description |
| ------------ | ------------ |
| composer lint | Check code on PSR
| composer stat-analyse | Run stat analyse
| composer test | Run tests
| composer mutate-test | Run mutation testing
| composer build-docs | Build documentation
| composer check-docs-actual | Check Documentation is actually
| composer build-changelog | Build CHANGELOG.MD
| composer lint | Check code on PSR |
| composer stat-analyse | Run stat analyse |
| composer test | Run tests |
| composer mutate-test | Run mutation testing |
| composer build-docs | Build documentation |
| composer check-docs-actual | Check Documentation is actually |
| composer build-changelog | Build CHANGELOG.MD |

## Count of code

| Type | Files' count | Code lines' count |
| ------------ | ------------ | ------------ |
| Source | 183 | 6680 |
| Tests | 118 | 7012 |

0 comments on commit e8bc297

Please sign in to comment.