Skip to content

Commit

Permalink
Merge e1b6f6e into 821fc85
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Apr 12, 2024
2 parents 821fc85 + e1b6f6e commit 7707202
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 77 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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)
<!-- auto-update:/top-badges -->

<!-- auto-update:rules-counter -->
Expand Down
50 changes: 15 additions & 35 deletions docker/build-preloader.php
Expand Up @@ -14,38 +14,18 @@

declare(strict_types=1);

$files = include_once __DIR__ . '/included_files.php';

$header = <<<'TEXT'
<?php
// if (!\function_exists('opcache_compile_file') ||
// !\ini_get('opcache.enable') ||
// !\ini_get('opcache.enable_cli')
// ) {
// echo 'Opcache is not available.';
// die(1);
// }
TEXT;

$result = [$header];

$excludes = [
'/app/csv-blueprint',
'/app/csv-blueprint.php',
];

foreach ($files as $path) {
foreach ($excludes as $exclude) {
if ($path === $exclude) {
continue 2;
}
}

// $result[] = "\\opcache_compile_file('{$path}');";
$result[] = "require_once '{$path}';";
}

echo 'Included classes:' . (\count($result) - 1) . \PHP_EOL;
\file_put_contents(__DIR__ . '/preload.php', \implode(\PHP_EOL, $result) . \PHP_EOL);
use JBZoo\CsvBlueprint\Tools\PreloadBuilder;

require_once __DIR__ . '/../vendor/autoload.php';

(new PreloadBuilder(isCompiler: true))
->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);
36 changes: 6 additions & 30 deletions docker/random-csv.php
Expand Up @@ -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');
6 changes: 3 additions & 3 deletions src/Rules/Cell/ComboPasswordStrength.php
Expand Up @@ -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++;
}

Expand All @@ -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++;
}

Expand Down
21 changes: 21 additions & 0 deletions src/Tools/Exception.php
@@ -0,0 +1,21 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Tools;

class Exception extends \RuntimeException
{
}
99 changes: 99 additions & 0 deletions src/Tools/PreloadBuilder.php
@@ -0,0 +1,99 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Tools;

use JBZoo\Utils\Cli;

final class PreloadBuilder
{
private array $files = [];
private array $excludes = [];

public function __construct(
private bool $isCompiler = false,
) {
}

public function saveToFile(string $filename, bool $showInfo = false): void
{
$classes = $this->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 = [
'<?php declare(strict_types=1);',
'',
];

if ($this->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;
}
}
52 changes: 52 additions & 0 deletions src/Tools/RandomCsvGenerator.php
@@ -0,0 +1,52 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Tools;

final class RandomCsvGenerator
{
private const MIN = 1;
private const MAX = 10_000;

public function __construct(
private int $rows,
private string $filePath,
private array $columns,
) {
}

public function generateCsv(): void
{
$fileHandle = \fopen($this->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);
}
}
26 changes: 19 additions & 7 deletions tests/PackageTest.php
Expand Up @@ -60,18 +60,19 @@ 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 = [
'github_actions',
'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
Expand Down Expand Up @@ -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',
),
);
Expand Down

0 comments on commit 7707202

Please sign in to comment.