Skip to content

Commit

Permalink
chore: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Oct 11, 2024
1 parent 551259f commit 17b2c39
Show file tree
Hide file tree
Showing 29 changed files with 190 additions and 175 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-faker": "^3.0",
"thecodingmachine/phpstan-safe-rule": "^1.2",
"rector/rector": "^0.18.13"
"rector/rector": "^1.0"
},
"config": {
"allow-plugins": {
Expand Down
33 changes: 18 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
->withPhpSets(php83:true)
->withAttributesSets()
->withPreparedSets(deadCode:true,typeDeclarations:true,codeQuality:true);
5 changes: 5 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor/bin/rector process src/ --config vendor/thecodingmachine/safe/rector-migrate.php
php-cs-fixer fix ./src --rules=@Symfony
php-cs-fixer fix ./test --rules=@Symfony
# XDEBUG_MODE=coverage ./vendor/bin/pest --coverage
# vendor/bin/phpstan analyse src --level 8
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Collection implements CollectionInterface
/**
* @var LoopCollectionInterface<TKey, T>
*/
private LoopCollectionInterface $collection;
private readonly LoopCollectionInterface $collection;

private function __construct(?LoopCollectionInterface $collection = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Config
{
public function __construct(
private bool $isUUID = false,
private readonly bool $isUUID = false,
private bool $isFrozen = false)
{
}
Expand Down
10 changes: 5 additions & 5 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ final class Database
* Create a new Database instance.
*/
public function __construct(
private Connection $connection,
private RecordManager $recordManager,
private WriteManager $writeManager,
private ModelManager $modelManager,
private Config $config,
private readonly Connection $connection,
private readonly RecordManager $recordManager,
private readonly WriteManager $writeManager,
private readonly ModelManager $modelManager,
private readonly Config $config,
) {
$this->registerJsonDocumentType();
}
Expand Down
12 changes: 3 additions & 9 deletions src/Factory/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,16 @@ public function wireContainer(array $connectionParams): void
*/
private function createConnection(array $connectionParams): void
{
$this->container->set(Connection::class, function () use ($connectionParams): Connection {
return DriverManager::getConnection($connectionParams);
});
$this->container->set(Connection::class, fn (): Connection => DriverManager::getConnection($connectionParams));
}

private function createModelManager(): void
{
$this->container->set(ModelManager::class, function (): ModelManager {
return new ModelManager($this->container);
});
$this->container->set(ModelManager::class, fn (): ModelManager => new ModelManager($this->container));
}

private function createConfig(bool $useUUID): void
{
$this->container->set(Config::class, function () use ($useUUID): Config {
return new Config($useUUID);
});
$this->container->set(Config::class, fn (): Config => new Config($useUUID));
}
}
2 changes: 1 addition & 1 deletion src/Manager/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class ModelManager
{
public function __construct(private \DI\Container $container)
public function __construct(private readonly \DI\Container $container)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/Manager/RecordManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ final class RecordManager
* Create RecordManager.
*/
public function __construct(
private Connection $connection,
private ModelManager $modelManager,
private Config $config,
private readonly Connection $connection,
private readonly ModelManager $modelManager,
private readonly Config $config,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions src/Manager/TableConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
class TableConstraint
{
public function __construct(
private string $foreignTableName,
private string $localColumnName,
private string $foreignColumnName,
private readonly string $foreignTableName,
private readonly string $localColumnName,
private readonly string $foreignColumnName,
) {
}

Expand Down
12 changes: 5 additions & 7 deletions src/Manager/TableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ final class TableManager
/**
* Store the instance of SchemaManager.
*/
private AbstractSchemaManager $manager;
private readonly AbstractSchemaManager $manager;

/**
* Store the instance of Platform.
*/
private \Doctrine\DBAL\Platforms\AbstractPlatform $platform;
private readonly \Doctrine\DBAL\Platforms\AbstractPlatform $platform;

/**
* create TableManager.
*/
public function __construct(
private Connection $connection,
private Config $config,
private readonly Connection $connection,
private readonly Config $config,
) {
$this->manager = $this->connection->createSchemaManager();
$this->platform = $this->connection->getDatabasePlatform();
Expand Down Expand Up @@ -199,7 +199,7 @@ public function tableExists(string $table_name): bool
$this->getTable($table_name);

return true;
} catch (TableDoesNotExist $e) {
} catch (TableDoesNotExist) {
return false;
}
}
Expand All @@ -216,7 +216,5 @@ public function saveOrUpdateTable(string $table_name, Table $new_table): void
}

$this->saveTable($new_table);

return;
}
}
14 changes: 7 additions & 7 deletions src/Manager/WriteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
final class WriteManager
{
public function __construct(
private Connection $connection,
private TableManager $tableManager,
private RecordManager $recordManager,
private ModelManager $modelManager,
private Config $config,
private readonly Connection $connection,
private readonly TableManager $tableManager,
private readonly RecordManager $recordManager,
private readonly ModelManager $modelManager,
private readonly Config $config,
) {
}

Expand Down Expand Up @@ -107,7 +107,7 @@ private function createConstraintsOto(Model $model): array
*
* @return array<TableConstraint>
*/
private function createConstraintsOtm(Model $model, Model $foreign): array
private function createConstraintsOtm(Model $model): array
{
$constraints = [];
array_push(
Expand Down Expand Up @@ -199,7 +199,7 @@ private function saveForeignOtm(Model $model): void
foreach ($model->getForeignModels('otm') as $foreign) {
$key = $model->getName().'_id';
$foreign->$key = $id;
$this->createTable($foreign, $this->createConstraintsOtm($model, $foreign));
$this->createTable($foreign, $this->createConstraintsOtm($model));
}
$this->connection->beginTransaction();
try {
Expand Down
22 changes: 11 additions & 11 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public function set(string $key, mixed $val): void

if (\Safe\preg_match('/[A-Z]/', $key)) {
$parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY);
if ('own' === strtolower($parts[0])) {
if ('own' === strtolower((string) $parts[0])) {
$this->__meta['foreign_models']['otm'] = $this->createCollection($this->__meta['foreign_models']['otm'], $val);
$this->__properties['all'][$key] = $val;

return;
}
if ('shared' === strtolower($parts[0])) {
if ('shared' === strtolower((string) $parts[0])) {
$this->__meta['foreign_models']['mtm'] = $this->createCollection($this->__meta['foreign_models']['mtm'], $val);
$this->__properties['all'][$key] = $val;

Expand Down Expand Up @@ -150,25 +150,25 @@ public function get(string $key): mixed
{
if (\Safe\preg_match('/[A-Z]/', $key)) {
$parts = \Safe\preg_split('/(?=[A-Z])/', $key, -1, PREG_SPLIT_NO_EMPTY);
if ('own' === strtolower($parts[0])) {
if ('list' === strtolower($parts[2])) {
$result = $this->recordManager->find(strtolower($parts[1]))->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get();
if ('own' === strtolower((string) $parts[0])) {
if ('list' === strtolower((string) $parts[2])) {
$result = $this->recordManager->find(strtolower((string) $parts[1]))->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get();
$this->set($key, $result);

return $result;
}
}
if ('shared' === strtolower($parts[0])) {
if ('list' === strtolower($parts[2])) {
$rel_table = $this->tableManager->tableExists($this->table.'_'.strtolower($parts[1])) ? $this->table.'_'.strtolower($parts[1]) : strtolower($parts[1]).'_'.$this->table;
if ('shared' === strtolower((string) $parts[0])) {
if ('list' === strtolower((string) $parts[2])) {
$rel_table = $this->tableManager->tableExists($this->table.'_'.strtolower((string) $parts[1])) ? $this->table.'_'.strtolower((string) $parts[1]) : strtolower((string) $parts[1]).'_'.$this->table;
$relations = $this->recordManager->find($rel_table)->where($this->getName().'_id = "'.$this->__meta['id'].'"')->get();
$rel_ids = '';
foreach ($relations as $relation) {
$key = strtolower($parts[1]).'_id';
$key = strtolower((string) $parts[1]).'_id';
$rel_ids .= "'".$relation->$key."',";
}
$rel_ids = substr($rel_ids, 0, -1);
$result = $this->recordManager->find(strtolower($parts[1]))->where('id IN ('.$rel_ids.')')->get();
$result = $this->recordManager->find(strtolower((string) $parts[1]))->where('id IN ('.$rel_ids.')')->get();
$this->set($key, $result);

return $result;
Expand Down Expand Up @@ -339,7 +339,7 @@ private function createCollection(?Collection $collection, array|Collection $mod
return $collection->merge($models);
}

if (count(array_filter($models, fn ($d) => !$d instanceof Model)) > 0) {
if (count(array_filter($models, fn ($d): bool => !$d instanceof Model)) > 0) {
throw new Exception\InvalidModelException();
}

Expand Down
Loading

0 comments on commit 17b2c39

Please sign in to comment.