Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker env + php-cs + phpstan + ci + fixes by php-cs-fixer and phpstan #26

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI
on: [push, pull_request]

jobs:
tests:
name: PHPUnit PHP ${{ matrix.php }} ${{ matrix.dependency }} (Symfony ${{ matrix.symfony }})
runs-on: ubuntu-22.04
strategy:
matrix:
php:
- '8.1'
- '8.2'
- '8.3'
symfony:
- '5.4.*'
- '6.4.*'
fail-fast: true
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Configure Symfony
run: composer config extra.symfony.require "${{ matrix.symfony }}"

- name: Update project dependencies
run: composer update --no-progress --ansi --prefer-stable

- name: Validate composer
run: composer validate --strict --no-check-lock

- name: PHP-CS-Fixer
run: vendor/bin/php-cs-fixer check -vv

- name: PHPStan
run: vendor/bin/phpstan analyse
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
composer.lock
composer.phar
.php_cs.cache
/vendor

# Other
Expand Down
14 changes: 14 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

return Retailcrm\PhpCsFixer\Defaults::rules([
'no_trailing_whitespace_in_string' => false,
])
->setFinder($finder)
->setCacheFile(__DIR__ . '/.php_cs.cache/results')
;
2 changes: 1 addition & 1 deletion Annotations/CustomIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @deprecated left only for automatic conversion of annotations to attributes
*
* @Annotation
* @Target("ANNOTATION")
*
Expand Down Expand Up @@ -47,5 +48,4 @@ class CustomIndex extends Annotation
* @var array
*/
public $columns;

}
5 changes: 3 additions & 2 deletions Annotations/CustomIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

/**
* @deprecated left only for automatic conversion of annotations to attributes
*
* @Annotation
* @Target("CLASS")
*/
class CustomIndexes extends Annotation
{
/**
* @CustomIndexes(indexes=[
*
* @CustomIndex(...),
* @CustomIndex(...),
* ...
* ])
**/
**/
public $indexes = [];

}
33 changes: 18 additions & 15 deletions Command/IndexUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

namespace Intaro\CustomIndexBundle\Command;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\EntityManagerInterface;
use Intaro\CustomIndexBundle\DTO\CustomIndex;
use Intaro\CustomIndexBundle\Metadata\ReaderInterface;
use Intaro\CustomIndexBundle\DBAL\ExtendedPlatform;
use Intaro\CustomIndexBundle\DBAL\QueryExecutor;
use Intaro\CustomIndexBundle\DTO\CustomIndex;
use Intaro\CustomIndexBundle\Metadata\ReaderInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -20,15 +21,15 @@ class IndexUpdateCommand extends Command
{
private const DUMP_SQL_OPTION = 'dump-sql';

private ?InputInterface $input;
private ?OutputInterface $output;
private InputInterface $input;
private OutputInterface $output;

public function __construct(
private readonly ValidatorInterface $validator,
private readonly EntityManagerInterface $em,
private readonly ReaderInterface $reader,
private readonly QueryExecutor $queryExecutor,
private readonly bool $searchInAllSchemas
private readonly bool $searchInAllSchemas,
) {
parent::__construct();
}
Expand Down Expand Up @@ -56,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* @param array<string> $indexesNames
* @param array<string> $indexesNames
* @param array<string, CustomIndex> $customIndexes
*/
private function createIndexes(array $indexesNames, array $customIndexes, ExtendedPlatform $platform): void
Expand All @@ -69,12 +70,12 @@ private function createIndexes(array $indexesNames, array $customIndexes, Extend
}
}
if (!$createFlag) {
$this->output->writeln("<info>No index was created</info>");
$this->output->writeln('<info>No index was created</info>');
}
}

/**
* @param array<string> $indexesNames
* @param array<string> $indexesNames
* @param array<string, CustomIndex> $customIndexes
*/
private function dropIndexes(array $indexesNames, array $customIndexes, ExtendedPlatform $platform): void
Expand All @@ -88,7 +89,7 @@ private function dropIndexes(array $indexesNames, array $customIndexes, Extended
}

if (!$dropFlag) {
$this->output->writeln("<info>No index was dropped.</info>");
$this->output->writeln('<info>No index was dropped.</info>');
}
}

Expand All @@ -101,7 +102,7 @@ private function dropIndex(ExtendedPlatform $platform, string $indexName): void
}

$this->queryExecutor->dropIndex($platform, $indexName);
$this->output->writeln("<info>Index ". $indexName ." was dropped.</info>");
$this->output->writeln('<info>Index ' . $indexName . ' was dropped.</info>');
}

private function createIndex(ExtendedPlatform $platform, CustomIndex $index): void
Expand All @@ -110,34 +111,36 @@ private function createIndex(ExtendedPlatform $platform, CustomIndex $index): vo
if (!count($errors)) {
if ($this->input->getOption(self::DUMP_SQL_OPTION)) {
$this->output->writeln($platform->createIndexSQL($index) . ';');

return;
}

$this->queryExecutor->createIndex($platform, $index);
$this->output->writeln("<info>Index ". $index->getName() ." was created.</info>");
$this->output->writeln('<info>Index ' . $index->getName() . ' was created.</info>');

return;
}

$this->output->writeln("<error>Index ". $index->getName() ." was not created.</error>");
$this->output->writeln('<error>Index ' . $index->getName() . ' was not created.</error>');

foreach ($errors as $error) {
$this->output->writeln("<error>". $error->getMessage() ."</error>");
$this->output->writeln('<error>' . $error->getMessage() . '</error>');
}
}

private function quoteSchema(string $name): string
{
$parts = explode('.', $name);
$parts[0] = '"'.$parts[0].'"';
$parts[0] = '"' . $parts[0] . '"';

return implode('.', $parts);
}

private function createExtendedPlatform(AbstractPlatform $platform): ExtendedPlatform
{
return match (true) {
$platform instanceof PostgreSQLPlatform => new ExtendedPlatform(),
default => throw new \LogicException(sprintf("Platform %s does not support", $platform::class)),
default => throw new \LogicException(sprintf('Platform %s does not support', $platform::class)),
};
}
}
14 changes: 7 additions & 7 deletions DBAL/ExtendedPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ final class ExtendedPlatform extends PostgreSQLPlatform
{
public function createIndexSQL(CustomIndex $index): string
{
$sql = 'CREATE ';
$sql = 'CREATE';
if ($index->getUnique()) {
$sql .= 'UNIQUE ';
$sql .= ' UNIQUE';
}

$sql .= 'INDEX ' . $index->getName() . ' ';
$sql .= 'ON ' . $index->getTableName() . ' ';
$sql .= ' INDEX ' . $index->getName();
$sql .= ' ON ' . $index->getTableName();

if ($index->getUsing()) {
$sql .= 'USING ' . $index->getUsing() . ' ';
$sql .= ' USING ' . $index->getUsing();
}

$sql .= '(' . implode(', ', $index->getColumns()) . ')';
$sql .= ' (' . implode(', ', $index->getColumns()) . ')';

if ($index->getWhere()) {
$sql .= ' WHERE ' . $index->getWhere();
Expand All @@ -44,7 +44,7 @@ public function indexesNamesSelectSQL(bool $searchInAllSchemas): string
{
$sql = "SELECT schemaname || '.' || indexname as relname FROM pg_indexes WHERE indexname LIKE :indexName";
if (!$searchInAllSchemas) {
$sql .= " AND schemaname = current_schema()";
$sql .= ' AND schemaname = current_schema()';
}

return $sql;
Expand Down
21 changes: 16 additions & 5 deletions DTO/CustomIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ final class CustomIndex

#[Assert\Length(min: 1, max: 63, minMessage: 'Name must be set', maxMessage: 'Name is too long')]
private ?string $name;

/** @var string[] */
#[Assert\Count(min: 1, minMessage: 'You must specify at least one column')]
#[Assert\All([
new Assert\Type([
'type' =>'string',
'type' => 'string',
'message' => 'Column should be type of string',
]),
])]
private array $columns = [];

private bool $unique;
#[AllowedIndexType]
private ?string $using;
Expand All @@ -29,6 +32,9 @@ final class CustomIndex
private string $schema;
private string $currentSchema;

/**
* @param string[]|string $columns
*/
public function __construct(
string $tableName,
string $schema,
Expand Down Expand Up @@ -75,6 +81,9 @@ public function getName(): ?string
return $this->name;
}

/**
* @return string[]
*/
public function getColumns(): array
{
return $this->columns;
Expand Down Expand Up @@ -104,12 +113,14 @@ private function generateName(): void
}

$strToMd5 .= $this->getUsing() . ($this->getWhere() ? '_' . $this->getWhere() : '');
$name = self::PREFIX . ( $this->getUnique() ? self::UNIQUE . '_' : '' ) . md5($strToMd5);
$name = self::PREFIX . ($this->getUnique() ? self::UNIQUE . '_' : '') . md5($strToMd5);
$this->setName($name);

}

private function setColumns($columns): void
/**
* @param string[]|string $columns
*/
private function setColumns(array|string $columns): void
{
if (is_string($columns)) {
$columns = [$columns];
Expand All @@ -123,7 +134,7 @@ private function setColumns($columns): void
}
}

private function setName($name): void
private function setName(string $name): void
{
if (!str_starts_with($name, self::PREFIX)) {
$name = self::PREFIX . $name;
Expand Down
8 changes: 3 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ class Configuration implements ConfigurationInterface
{
private const AVAILABLE_INDEX_TYPES = ['btree', 'hash', 'gin', 'gist'];

/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('intaro_custom_index');
Expand All @@ -27,13 +24,14 @@ public function getConfigTreeBuilder(): TreeBuilder
->prototype('scalar')
->validate()
->ifNotInArray(self::AVAILABLE_INDEX_TYPES)
->thenInvalid("Unknown index type. Allowed types: ".implode(', ', self::AVAILABLE_INDEX_TYPES).".")
->thenInvalid('Unknown index type. Allowed types: ' . implode(', ', self::AVAILABLE_INDEX_TYPES) . '.')
->end()
->end()
->cannotBeEmpty()
->defaultValue(self::AVAILABLE_INDEX_TYPES)
->end()
->end();
->end()
;

return $treeBuilder;
}
Expand Down
7 changes: 2 additions & 5 deletions DependencyInjection/IntaroCustomIndexExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

namespace Intaro\CustomIndexBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class IntaroCustomIndexExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(dirname(__DIR__).'/config'));
$loader = new PhpFileLoader($container, new FileLocator(dirname(__DIR__) . '/config'));
$loader->load('di.php');

$configuration = new Configuration();
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG PHP_IMAGE_TAG
FROM php:${PHP_IMAGE_TAG}-cli-alpine

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /opt/test
2 changes: 1 addition & 1 deletion IntaroCustomIndexBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

class IntaroCustomIndexBundle extends Bundle
{
}
}
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PHP=docker-compose run --rm --no-deps php

vendor: composer.json
@$(PHP) composer install -o -n --no-ansi
@touch vendor || true

php-cs: vendor
@$(PHP) vendor/bin/php-cs-fixer fix --using-cache=no -vv

phpstan: vendor
@$(PHP) vendor/bin/phpstan analyse

check: php-cs phpstan
Loading