diff --git a/README.md b/README.md index 9e5abbff..23af2630 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ [![CI](https://github.com/JBZoo/Csv-Blueprint/actions/workflows/demo.yml/badge.svg)](https://github.com/JBZoo/Csv-Blueprint/actions/workflows/demo.yml) [![Coverage Status](https://coveralls.io/repos/github/JBZoo/Csv-Blueprint/badge.svg?branch=master)](https://coveralls.io/github/JBZoo/Csv-Blueprint?branch=master) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Csv-Blueprint/coverage.svg)](https://shepherd.dev/github/JBZoo/Csv-Blueprint) -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=alert_status)](https://sonarcloud.io/summary/overall?id=JBZoo_Csv-Blueprint) +[![Sonar - Bugs](https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=bugs)](https://sonarcloud.io/summary/overall?id=JBZoo_Csv-Blueprint) +[![Sonar - Code smells](https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=code_smells)](https://sonarcloud.io/summary/overall?id=JBZoo_Csv-Blueprint) [![Docker Pulls](https://img.shields.io/docker/pulls/jbzoo/csv-blueprint.svg)](https://hub.docker.com/r/jbzoo/csv-blueprint/tags) -[![GitHub Release](https://img.shields.io/github/v/release/jbzoo/csv-blueprint?label=Latest)](https://github.com/jbzoo/csv-blueprint/releases) diff --git a/docker/build-preloader.php b/docker/build-preloader.php index 562749db..fc3cc847 100644 --- a/docker/build-preloader.php +++ b/docker/build-preloader.php @@ -14,38 +14,18 @@ declare(strict_types=1); -$files = include_once __DIR__ . '/included_files.php'; - -$header = <<<'TEXT' - setExcludes([ + '/app/csv-blueprint', + '/app/csv-blueprint.php', + ]) + ->setFiles( + \file_exists(__DIR__ . '/included_files.php') + ? include_once __DIR__ . '/included_files.php' + : \array_values(include_once __DIR__ . '/../vendor/composer/autoload_classmap.php'), + ) + ->saveToFile(__DIR__ . '/preload.php', true); diff --git a/docker/random-csv.php b/docker/random-csv.php index b1d4596c..7ba0842d 100644 --- a/docker/random-csv.php +++ b/docker/random-csv.php @@ -14,39 +14,15 @@ declare(strict_types=1); -final class CsvGenerator -{ - public function __construct( - private int $rows, - private string $filePath, - private array $columns, - ) { - } +use JBZoo\CsvBlueprint\Tools\RandomCsvGenerator; +use JBZoo\Utils\Cli; - public function generateCsv(): void - { - $fileHandle = \fopen($this->filePath, 'w'); +require_once __DIR__ . '/../vendor/autoload.php'; - \fputcsv($fileHandle, $this->columns); - - for ($i = 0; $i < $this->rows; $i++) { - $rowData = []; - - foreach (\array_keys($this->columns) as $columnIndex) { - $rowData[$columnIndex] = \random_int(1, 10000); - } - - \fputcsv($fileHandle, $rowData); - } - - \fclose($fileHandle); - - echo "CSV file created: {$this->filePath}.\n"; - } -} - -(new CsvGenerator( +(new RandomCsvGenerator( 1000, __DIR__ . '/random_data.csv', ['Column Name (header)', 'another_column', 'inherited_column_login', 'inherited_column_full_name'], ))->generateCsv(); + +Cli::out('Random CSV file with 1000 lines created: ' . __DIR__ . '/random_data.csv'); diff --git a/src/Rules/Cell/ComboPasswordStrength.php b/src/Rules/Cell/ComboPasswordStrength.php index 052d30fc..31957643 100644 --- a/src/Rules/Cell/ComboPasswordStrength.php +++ b/src/Rules/Cell/ComboPasswordStrength.php @@ -49,12 +49,12 @@ public static function passwordScore(string $password): int $score += \min(5, \strlen($password) / 2); // Uppercase letters: +1 point if at least one - if (\preg_match('/[A-Z]/', $password) !== 0) { + if (\preg_match('/[A-Z]/', $password) !== 0) { // NOSONAR $score++; } // Lowercase letters: +1 point if at least one - if (\preg_match('/[a-z]/', $password) !== 0) { + if (\preg_match('/[a-z]/', $password) !== 0) { // NOSONAR $score++; } @@ -64,7 +64,7 @@ public static function passwordScore(string $password): int } // Special characters: +1 point if at least one - if (\preg_match('/[^a-zA-Z0-9]/', $password) !== 0) { + if (\preg_match('/[^a-zA-Z0-9]/', $password) !== 0) { // NOSONAR $score++; } diff --git a/src/Tools/Exception.php b/src/Tools/Exception.php new file mode 100644 index 00000000..32f6c35c --- /dev/null +++ b/src/Tools/Exception.php @@ -0,0 +1,21 @@ +buildClassList(); + $lines = $this->buildHeader() + $classes; + + \file_put_contents($filename, \implode(\PHP_EOL, $lines) . \PHP_EOL); + + if ($showInfo) { + Cli::out('Included classes: ' . \count($classes)); + } + } + + public function setExcludes(array $excludes): self + { + $this->excludes = $excludes; + return $this; + } + + public function setFiles(array $files): self + { + $this->files = $files; + return $this; + } + + private function buildClassList(): array + { + $classes = []; + + foreach ($this->files as $path) { + if ($this->isExcluded($path)) { + continue; + } + + $classes[] = $this->isCompiler + ? "\\opcache_compile_file('{$path}');" + : "require_once '{$path}';"; + } + + return $classes; + } + + private function isExcluded(string $path): bool + { + return \in_array($path, $this->excludes, true); + } + + private function buildHeader(): array + { + $header = [ + 'isCompiler) { + $header = \array_merge($header, [ + "if (!\\function_exists('opcache_compile_file') ||", + " !\\ini_get('opcache.enable') ||", + " !\\ini_get('opcache.enable_cli')", + ') {', + " echo 'Opcache is not available.';", + ' die(1);', + '}', + '', + ]); + } + + return $header; + } +} diff --git a/src/Tools/RandomCsvGenerator.php b/src/Tools/RandomCsvGenerator.php new file mode 100644 index 00000000..86ad9ccc --- /dev/null +++ b/src/Tools/RandomCsvGenerator.php @@ -0,0 +1,52 @@ +filePath, 'w'); + if ($fileHandle === false) { + throw new Exception("Can't open file: {$this->filePath}"); + } + + \fputcsv($fileHandle, $this->columns); + + for ($i = 0; $i < $this->rows; $i++) { + $rowData = []; + + foreach (\array_keys($this->columns) as $columnIndex) { + $rowData[$columnIndex] = \random_int(self::MIN, self::MAX); + } + + \fputcsv($fileHandle, $rowData); + } + + \fclose($fileHandle); + } +} diff --git a/tests/PackageTest.php b/tests/PackageTest.php index 5619148e..04b3e8a7 100644 --- a/tests/PackageTest.php +++ b/tests/PackageTest.php @@ -60,7 +60,9 @@ final class PackageTest extends \JBZoo\Codestyle\PHPUnit\AbstractPackageTest 'sonarcloud' => true, 'coveralls' => true, 'circle_ci' => true, - 'sonar_qube' => true, + + 'sonar_qube_bugs' => true, + 'sonar_qube_smells' => true, ]; protected array $badgesTemplate = [ @@ -68,10 +70,9 @@ final class PackageTest extends \JBZoo\Codestyle\PHPUnit\AbstractPackageTest 'github_actions_demo', 'coveralls', 'psalm_coverage', - 'sonar_qube', - // 'packagist_downloads_total', + 'sonar_qube_bugs', + 'sonar_qube_smells', 'docker_pulls', - 'github_latest_release', ]; protected function setUp(): void @@ -177,12 +178,23 @@ protected function checkBadgeGithubLatestRelease(): ?string ); } - protected function checkBadgeSonarQube(): ?string + protected function checkBadgeSonarQubeBugs(): ?string + { + return $this->getPreparedBadge( + $this->getBadge( + 'Sonar - Bugs', + 'https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=bugs', + 'https://sonarcloud.io/summary/overall?id=JBZoo_Csv-Blueprint', + ), + ); + } + + protected function checkBadgeSonarQubeSmells(): ?string { return $this->getPreparedBadge( $this->getBadge( - 'Quality Gate Status', - 'https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=alert_status', + 'Sonar - Code smells', + 'https://sonarcloud.io/api/project_badges/measure?project=JBZoo_Csv-Blueprint&metric=code_smells', 'https://sonarcloud.io/summary/overall?id=JBZoo_Csv-Blueprint', ), ); diff --git a/tests/Tools/PreloadBuilderTest.php b/tests/Tools/PreloadBuilderTest.php new file mode 100644 index 00000000..7279f4b7 --- /dev/null +++ b/tests/Tools/PreloadBuilderTest.php @@ -0,0 +1,40 @@ +setExcludes([ + __FILE__, + ]) + ->setFiles(\get_included_files()) + ->saveToFile(PROJECT_BUILD . '/preload.php'); + + self::assertFileExists(PROJECT_BUILD . '/preload.php'); + + $content = \file_get_contents(PROJECT_BUILD . '/preload.php'); + isNotContain(__FILE__, $content); + } +} diff --git a/tests/Tools/RandomCsvGeneratorTest.php b/tests/Tools/RandomCsvGeneratorTest.php new file mode 100644 index 00000000..c1992935 --- /dev/null +++ b/tests/Tools/RandomCsvGeneratorTest.php @@ -0,0 +1,38 @@ +generateCsv(); + + self::assertFileExists($outputFile); + + $content = \file_get_contents($outputFile); + self::assertStringContainsString("\"col 1\",\"col 2\",\"col 3\",\"col 4\"\n", $content); + } +}