diff --git a/composer.json b/composer.json index b163c89..af3d2bb 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=8.0", "infinityloop-dev/utils": "^2.1", "nette/utils": "^3.0", - "nette/database": "^3.0", + "nette/database": "^3.1", "nette/robot-loader": "^3.3", "symfony/console": "^5.2" }, @@ -32,7 +32,7 @@ "mockery/mockery": "^1.4", "infection/infection": "^0.20", "phpstan/phpstan": "^0.12", - "infinityloop-dev/coding-standard": "^0.1" + "infinityloop-dev/coding-standard": "^0.2" }, "autoload": { "psr-4": { diff --git a/src/Attribute/ClassUniqueConstraint.php b/src/Attribute/ClassUniqueConstraint.php index ecb6082..6a66b73 100644 --- a/src/Attribute/ClassUniqueConstraint.php +++ b/src/Attribute/ClassUniqueConstraint.php @@ -4,11 +4,11 @@ namespace CoolBeans\Attribute; -#[\Attribute(\Attribute::TARGET_CLASS|\Attribute::IS_REPEATABLE)] +#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] final class ClassUniqueConstraint { public function __construct( - public array $columns + public array $columns, ) { if (\count($columns) < 2) { diff --git a/src/Attribute/DefaultValue.php b/src/Attribute/DefaultValue.php index 9e5a0cc..464b719 100644 --- a/src/Attribute/DefaultValue.php +++ b/src/Attribute/DefaultValue.php @@ -8,6 +8,8 @@ final class DefaultValue { public function __construct( - public string $defaultValue - ) {} + public string $defaultValue, + ) + { + } } diff --git a/src/Attribute/TypeOverride.php b/src/Attribute/TypeOverride.php index 1c32989..9485533 100644 --- a/src/Attribute/TypeOverride.php +++ b/src/Attribute/TypeOverride.php @@ -11,7 +11,7 @@ final class TypeOverride public function __construct( public string $type, - int ...$lengthArgs + int ...$lengthArgs, ) { $this->lengthArgs = $lengthArgs; diff --git a/src/Bean.php b/src/Bean.php index 3d94798..bcc1372 100644 --- a/src/Bean.php +++ b/src/Bean.php @@ -4,21 +4,19 @@ namespace CoolBeans; -use \CoolBeans\Contract\PrimaryKey; - abstract class Bean implements \CoolBeans\Contract\Row, \IteratorAggregate { use \Nette\SmartObject; - protected \Nette\Database\Table\ActiveRow $row; protected \ReflectionClass $reflection; - protected PrimaryKey $primaryKey; + protected \CoolBeans\Contract\PrimaryKey $primaryKey; - public function __construct(\Nette\Database\Table\ActiveRow $row) + public function __construct( + protected \Nette\Database\Table\ActiveRow $row, + ) { - $this->row = $row; $this->reflection = new \ReflectionClass(static::class); - $this->primaryKey = PrimaryKey::create($this->row); + $this->primaryKey = \CoolBeans\Contract\PrimaryKey::create($this->row); if (\CoolBeans\Config::$validateColumns) { $this->validateMissingColumns(); @@ -50,7 +48,7 @@ public function toArray() : array /** * Returns primary key object. */ - public function getPrimaryKey() : PrimaryKey + public function getPrimaryKey() : \CoolBeans\Contract\PrimaryKey { return $this->primaryKey; } @@ -84,7 +82,7 @@ public function offsetExists($offset) : bool $property = $this->reflection->getProperty($offset); return $property->isPublic(); - } catch (\ReflectionException $e) { + } catch (\ReflectionException) { return false; } } diff --git a/src/Bridge/Nette/DataSource.php b/src/Bridge/Nette/DataSource.php index 34f6ccb..a7f3f0e 100644 --- a/src/Bridge/Nette/DataSource.php +++ b/src/Bridge/Nette/DataSource.php @@ -4,11 +4,9 @@ namespace CoolBeans\Bridge\Nette; -use \CoolBeans\Contract\PrimaryKey; - interface DataSource extends \CoolBeans\Contract\DataSource { - public function getRow(PrimaryKey $key) : \CoolBeans\Bridge\Nette\ActiveRow; + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Bridge\Nette\ActiveRow; public function findAll() : \CoolBeans\Bridge\Nette\Selection; diff --git a/src/Bridge/Nette/Selection.php b/src/Bridge/Nette/Selection.php index 6aef7a0..7bcdad4 100644 --- a/src/Bridge/Nette/Selection.php +++ b/src/Bridge/Nette/Selection.php @@ -6,6 +6,21 @@ class Selection extends \Nette\Database\Table\Selection implements \CoolBeans\Contract\Selection { + public function where($condition, ...$params) : static + { + return parent::where($condition, ...$params); + } + + public function fetch() : ?\CoolBeans\Bridge\Nette\ActiveRow + { + return parent::fetch(); + } + + public function key() : string|int + { + return parent::key(); + } + public function getTableName() : string { return $this->getName(); @@ -13,9 +28,8 @@ public function getTableName() : string public function current() : ?\CoolBeans\Bridge\Nette\ActiveRow { - return ($key = \current($this->keys)) !== false - ? $this->data[$key] - : null; + return parent::current() + ?: null; } protected function createRow(array $row) : \CoolBeans\Bridge\Nette\ActiveRow diff --git a/src/Bridge/Nette/TDecorator.php b/src/Bridge/Nette/TDecorator.php index 1e40099..7bff88e 100644 --- a/src/Bridge/Nette/TDecorator.php +++ b/src/Bridge/Nette/TDecorator.php @@ -4,15 +4,13 @@ namespace CoolBeans\Bridge\Nette; -use \CoolBeans\Contract\PrimaryKey; - trait TDecorator { use \CoolBeans\Decorator\TCommon; protected \CoolBeans\Bridge\Nette\DataSource $dataSource; - public function getRow(PrimaryKey $key) : \CoolBeans\Bridge\Nette\ActiveRow + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Bridge\Nette\ActiveRow { return $this->dataSource->getRow($key); } diff --git a/src/Command/SqlGeneratorCommand.php b/src/Command/SqlGeneratorCommand.php index f40da5d..3efb5af 100644 --- a/src/Command/SqlGeneratorCommand.php +++ b/src/Command/SqlGeneratorCommand.php @@ -24,7 +24,7 @@ protected function configure() : void protected function execute( \Symfony\Component\Console\Input\InputInterface $input, - \Symfony\Component\Console\Output\OutputInterface $output + \Symfony\Component\Console\Output\OutputInterface $output, ) : int { $converted = ''; @@ -113,8 +113,7 @@ private function buildTable(array $data) : string . $row['name'] . \str_repeat(' ', $longestNameLength - $nameLength + 1) . $row['dataType'] . \str_repeat(' ', $longestDataTypeLength - $dataTypeLength + 1) . $row['notNull'] - . $row['default'] - ); + . $row['default']); } return \implode(',' . \PHP_EOL, $toReturn); @@ -315,7 +314,6 @@ private function getBeans(string $destination) : array throw new \CoolBeans\Exception\BeanWithoutPublicProperty('Bean ' . $bean->getShortName() . ' has no public property.'); } - $beans[] = $class; } diff --git a/src/Contract/ContextFactory.php b/src/Contract/ContextFactory.php index af0084c..a3cf1d2 100644 --- a/src/Contract/ContextFactory.php +++ b/src/Contract/ContextFactory.php @@ -6,5 +6,5 @@ interface ContextFactory { - public function create() : \Nette\Database\Context; + public function create() : \Nette\Database\Explorer; } diff --git a/src/Contract/DataSource.php b/src/Contract/DataSource.php index a5ee583..a80b403 100644 --- a/src/Contract/DataSource.php +++ b/src/Contract/DataSource.php @@ -67,5 +67,5 @@ public function upsert(?\CoolBeans\Contract\PrimaryKey $key, array $values) : \C /** * Executes function enclosed in PDO transaction. */ - public function transaction(callable $function); + public function transaction(callable $function) : mixed; } diff --git a/src/Contract/Selection.php b/src/Contract/Selection.php index 4fef8ac..4f3a5ca 100644 --- a/src/Contract/Selection.php +++ b/src/Contract/Selection.php @@ -8,9 +8,9 @@ interface Selection extends \Iterator, \Countable { public function getTableName() : string; - public function fetch(); // : ?\CoolBeans\Contract\Row + public function fetch() : ?\CoolBeans\Contract\Row; - public function where(string $column, string|int|array ...$val); // : static + public function where(string $column, string|int|array ...$val) : static; public function count() : int; @@ -18,7 +18,7 @@ public function rewind() : void; public function valid() : bool; - public function key(); // : string|int + public function key() : string|int; public function current() : ?\CoolBeans\Contract\Row; diff --git a/src/DataSource.php b/src/DataSource.php index 6eb48b6..933d0f7 100644 --- a/src/DataSource.php +++ b/src/DataSource.php @@ -4,11 +4,9 @@ namespace CoolBeans; -use \CoolBeans\Contract\PrimaryKey; - interface DataSource extends \CoolBeans\Contract\DataSource { - public function getRow(PrimaryKey $key) : \CoolBeans\Bean; + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Bean; public function findAll() : \CoolBeans\Selection; diff --git a/src/Decorator/Active.php b/src/Decorator/Active.php index 00ef345..b6448e6 100644 --- a/src/Decorator/Active.php +++ b/src/Decorator/Active.php @@ -4,8 +4,6 @@ namespace CoolBeans\Decorator; -use \CoolBeans\Contract\PrimaryKey; - final class Active implements \CoolBeans\Contract\DataSource { use \Nette\SmartObject; @@ -16,7 +14,7 @@ public function __construct(\CoolBeans\Contract\DataSource $dataSource) $this->dataSource = $dataSource; } - public function getRow(PrimaryKey $key) : \CoolBeans\Contract\Row + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Contract\Row { $row = $this->dataSource->getRow($key); @@ -39,7 +37,7 @@ public function findByArray(array $filter) : \CoolBeans\Contract\Selection ->where($this->getName() . '.active >= ?', 0); } - public function delete(PrimaryKey $key) : \CoolBeans\Result\Delete + public function delete(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Result\Delete { $this->dataSource->update($key, ['active' => -1]); diff --git a/src/Decorator/Bean.php b/src/Decorator/Bean.php index abfb0af..9f1899d 100644 --- a/src/Decorator/Bean.php +++ b/src/Decorator/Bean.php @@ -4,30 +4,24 @@ namespace CoolBeans\Decorator; -use \CoolBeans\Contract\PrimaryKey; - final class Bean implements \CoolBeans\DataSource { use \Nette\SmartObject; use \CoolBeans\Decorator\TCommon; - private \CoolBeans\Contract\DataSource $dataSource; - private string $rowClass; - private string $selectionClass; - - public function __construct(\CoolBeans\Contract\DataSource $dataSource, string $rowClass, string $selectionClass) + public function __construct( + private \CoolBeans\Contract\DataSource $dataSource, + private string $rowClass, + private string $selectionClass, + ) { if (!\is_subclass_of($rowClass, \CoolBeans\Bean::class) || !\is_subclass_of($selectionClass, \CoolBeans\Selection::class)) { throw new \CoolBeans\Exception\InvalidFunctionParameters('Bean decorator can transform only to Bean instance.'); } - - $this->dataSource = $dataSource; - $this->rowClass = $rowClass; - $this->selectionClass = $selectionClass; } - public function getRow(PrimaryKey $entryId) : \CoolBeans\Bean + public function getRow(\CoolBeans\Contract\PrimaryKey $entryId) : \CoolBeans\Bean { return $this->createRow($this->dataSource->getRow($entryId)); } diff --git a/src/Decorator/History.php b/src/Decorator/History.php index bb57b43..ab69fcf 100644 --- a/src/Decorator/History.php +++ b/src/Decorator/History.php @@ -4,8 +4,6 @@ namespace CoolBeans\Decorator; -use \CoolBeans\Contract\PrimaryKey; - final class History implements \CoolBeans\Contract\DataSource { use \Nette\SmartObject; @@ -13,24 +11,17 @@ final class History implements \CoolBeans\Contract\DataSource public const METADATA = ['id', 'active']; - private \CoolBeans\Contract\DataSource $historyDataSource; - private array $additionalData; - private array $metadata; - public function __construct( \CoolBeans\Contract\DataSource $dataSource, - \CoolBeans\Contract\DataSource $historyDataSource, - array $additionalData = [], - array $metadata = self::METADATA + private \CoolBeans\Contract\DataSource $historyDataSource, + private array $additionalData = [], + private array $metadata = self::METADATA, ) { $this->dataSource = $dataSource; - $this->historyDataSource = $historyDataSource; - $this->additionalData = $additionalData; - $this->metadata = $metadata; } - public function update(PrimaryKey $key, array $data) : \CoolBeans\Result\Update + public function update(\CoolBeans\Contract\PrimaryKey $key, array $data) : \CoolBeans\Result\Update { if ($this->isMetadataChange($data)) { return $this->dataSource->update($key, $data); @@ -87,7 +78,7 @@ private function isMetadataChange(array $data) : bool return true; } - private function insertHistory(PrimaryKey $currentKey, array $oldData) : PrimaryKey + private function insertHistory(\CoolBeans\Contract\PrimaryKey $currentKey, array $oldData) : \CoolBeans\Contract\PrimaryKey { $oldData[$this->getName() . '_id'] = $currentKey->getValue(); diff --git a/src/Decorator/TDecorator.php b/src/Decorator/TDecorator.php index 03fdb1d..c162d03 100644 --- a/src/Decorator/TDecorator.php +++ b/src/Decorator/TDecorator.php @@ -4,15 +4,13 @@ namespace CoolBeans\Decorator; -use \CoolBeans\Contract\PrimaryKey; - trait TDecorator { use \CoolBeans\Decorator\TCommon; protected \CoolBeans\Contract\DataSource $dataSource; - public function getRow(PrimaryKey $key) : \CoolBeans\Contract\Row + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Contract\Row { return $this->dataSource->getRow($key); } diff --git a/src/PrimaryKey/IntPrimaryKey.php b/src/PrimaryKey/IntPrimaryKey.php index f3ccd24..9cbd4fe 100644 --- a/src/PrimaryKey/IntPrimaryKey.php +++ b/src/PrimaryKey/IntPrimaryKey.php @@ -7,16 +7,17 @@ final class IntPrimaryKey extends \CoolBeans\Contract\PrimaryKey { private int $value; - private string $name; - public function __construct(int $key, string $name = 'id') + public function __construct( + int $key, + private string $name = 'id', + ) { if ($key <= 0) { throw new \CoolBeans\Exception\InvalidFunctionParameters('Primary key must be positive integer.'); } $this->value = $key; - $this->name = $name; } public function getValue() : int @@ -36,8 +37,8 @@ public function getName() : string public function equals(\CoolBeans\Contract\PrimaryKey $compare) : bool { - return $compare instanceof self - && $this->getValue() === $compare->getValue() + return $compare instanceof self + && $this->getValue() === $compare->getValue() && $this->getName() === $compare->getName(); } } diff --git a/src/Result/Delete.php b/src/Result/Delete.php index d3e75d4..d5da33e 100644 --- a/src/Result/Delete.php +++ b/src/Result/Delete.php @@ -4,16 +4,13 @@ namespace CoolBeans\Result; -use \CoolBeans\Contract\PrimaryKey; - class Delete implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public PrimaryKey $deletedId; - - public function __construct(PrimaryKey $deletedId) + public function __construct( + public \CoolBeans\Contract\PrimaryKey $deletedId, + ) { - $this->deletedId = $deletedId; } } diff --git a/src/Result/DeleteByArray.php b/src/Result/DeleteByArray.php index a9a2e7b..dee8d35 100644 --- a/src/Result/DeleteByArray.php +++ b/src/Result/DeleteByArray.php @@ -8,10 +8,9 @@ class DeleteByArray implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public array $deletedIds; - - public function __construct(array $deletedIds) + public function __construct( + public array $deletedIds, + ) { - $this->deletedIds = $deletedIds; } } diff --git a/src/Result/HistoryUpdate.php b/src/Result/HistoryUpdate.php index ab5b6a2..ec3b3e8 100644 --- a/src/Result/HistoryUpdate.php +++ b/src/Result/HistoryUpdate.php @@ -4,15 +4,14 @@ namespace CoolBeans\Result; -use \CoolBeans\Contract\PrimaryKey; - class HistoryUpdate extends \CoolBeans\Result\Update { - public ?PrimaryKey $historyId; - - public function __construct(PrimaryKey $updatedId, bool $dataChanged, ?PrimaryKey $historyId = null) + public function __construct( + \CoolBeans\Contract\PrimaryKey $updatedId, + bool $dataChanged, + public ?\CoolBeans\Contract\PrimaryKey $historyId = null, + ) { parent::__construct($updatedId, $dataChanged); - $this->historyId = $historyId; } } diff --git a/src/Result/HistoryUpdateByArray.php b/src/Result/HistoryUpdateByArray.php index 99dee07..a76feb9 100644 --- a/src/Result/HistoryUpdateByArray.php +++ b/src/Result/HistoryUpdateByArray.php @@ -6,11 +6,12 @@ class HistoryUpdateByArray extends \CoolBeans\Result\UpdateByArray { - public array $historyIds; - - public function __construct(array $updatedIds, array $changedIds, array $historyIds) + public function __construct( + array $updatedIds, + array $changedIds, + public array $historyIds, + ) { parent::__construct($updatedIds, $changedIds); - $this->historyIds = $historyIds; } } diff --git a/src/Result/Insert.php b/src/Result/Insert.php index 77429c1..8a6c2aa 100644 --- a/src/Result/Insert.php +++ b/src/Result/Insert.php @@ -8,10 +8,9 @@ class Insert implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public \CoolBeans\Contract\PrimaryKey $insertedId; - - public function __construct(\CoolBeans\Contract\PrimaryKey $insertedId) + public function __construct( + public \CoolBeans\Contract\PrimaryKey $insertedId, + ) { - $this->insertedId = $insertedId; } } diff --git a/src/Result/InsertMultiple.php b/src/Result/InsertMultiple.php index 8edf2bb..f785508 100644 --- a/src/Result/InsertMultiple.php +++ b/src/Result/InsertMultiple.php @@ -8,10 +8,9 @@ class InsertMultiple implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public array $insertedIds; - - public function __construct(array $insertedIds) + public function __construct( + public array $insertedIds, + ) { - $this->insertedIds = $insertedIds; } } diff --git a/src/Result/Update.php b/src/Result/Update.php index 5ea8fa8..5a68942 100644 --- a/src/Result/Update.php +++ b/src/Result/Update.php @@ -4,18 +4,14 @@ namespace CoolBeans\Result; -use \CoolBeans\Contract\PrimaryKey; - class Update implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public PrimaryKey $updatedId; - public bool $dataChanged; - - public function __construct(PrimaryKey $updatedId, bool $dataChanged) + public function __construct( + public \CoolBeans\Contract\PrimaryKey $updatedId, + public bool $dataChanged, + ) { - $this->updatedId = $updatedId; - $this->dataChanged = $dataChanged; } } diff --git a/src/Result/UpdateByArray.php b/src/Result/UpdateByArray.php index 7280e83..271e66e 100644 --- a/src/Result/UpdateByArray.php +++ b/src/Result/UpdateByArray.php @@ -8,12 +8,10 @@ class UpdateByArray implements \CoolBeans\Contract\Result { use \Nette\SmartObject; - public array $updatedIds; - public array $changedIds; - - public function __construct(array $updatedIds, array $changedIds) + public function __construct( + public array $updatedIds, + public array $changedIds, + ) { - $this->updatedIds = $updatedIds; - $this->changedIds = $changedIds; } } diff --git a/src/Selection.php b/src/Selection.php index 31a8aa2..c21a7b4 100644 --- a/src/Selection.php +++ b/src/Selection.php @@ -13,14 +13,14 @@ abstract class Selection implements \CoolBeans\Contract\Selection protected \ReflectionClass $reflection; final public function __construct( - protected \Nette\Database\Table\Selection $selection + protected \Nette\Database\Table\Selection $selection, ) { $this->reflection = new \ReflectionClass(static::class); - if (\CoolBeans\Config::$validateTableName) { - $this->validateTableName(); - } + if (\CoolBeans\Config::$validateTableName) { + $this->validateTableName(); + } } /** diff --git a/src/TDecorator.php b/src/TDecorator.php index 86586f2..10edec9 100644 --- a/src/TDecorator.php +++ b/src/TDecorator.php @@ -4,15 +4,13 @@ namespace CoolBeans; -use \CoolBeans\Contract\PrimaryKey; - trait TDecorator { use \CoolBeans\Decorator\TCommon; protected \CoolBeans\DataSource $dataSource; - public function getRow(PrimaryKey $key) : \CoolBeans\Bean + public function getRow(\CoolBeans\Contract\PrimaryKey $key) : \CoolBeans\Bean { return $this->dataSource->getRow($key); } diff --git a/src/Table.php b/src/Table.php index 6a2851c..5bf0615 100644 --- a/src/Table.php +++ b/src/Table.php @@ -8,23 +8,15 @@ class Table implements \CoolBeans\Bridge\Nette\DataSource { use \Nette\SmartObject; - protected string $tableName; - protected ?\Nette\Database\Explorer $context = null; - protected ?\CoolBeans\Contract\ContextFactory $contextFactory = null; - public function __construct( - string $tableName, - ?\Nette\Database\Explorer $context = null, - ?\CoolBeans\Contract\ContextFactory $contextFactory = null + protected string $tableName, + protected ?\Nette\Database\Explorer $context = null, + protected ?\CoolBeans\Contract\ContextFactory $contextFactory = null, ) { if ($context === null && $contextFactory === null) { throw new \CoolBeans\Exception\InvalidFunctionParameters('Either context or its factory must be provided.'); } - - $this->tableName = $tableName; - $this->context = $context; - $this->contextFactory = $contextFactory; } public function getName() : string diff --git a/tests/Unit/TestBean/AttributeBean.php b/tests/Unit/TestBean/AttributeBean.php index b4eddb8..ef41bdd 100644 --- a/tests/Unit/TestBean/AttributeBean.php +++ b/tests/Unit/TestBean/AttributeBean.php @@ -6,6 +6,7 @@ //@phpcs:disable SlevomatCodingStandard.Classes.ClassStructure.IncorrectGroupOrder //@phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty +//@phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps #[\CoolBeans\Attribute\ClassUniqueConstraint(['col2', 'col3'])] #[\CoolBeans\Attribute\ClassUniqueConstraint(['col4', 'col5', 'col6'])] final class AttributeBean extends \CoolBeans\Bean diff --git a/tests/Unit/TestBean/SimpleBean1.php b/tests/Unit/TestBean/SimpleBean1.php index 7746348..183be8f 100644 --- a/tests/Unit/TestBean/SimpleBean1.php +++ b/tests/Unit/TestBean/SimpleBean1.php @@ -6,6 +6,7 @@ //@phpcs:disable SlevomatCodingStandard.Classes.ClassStructure.IncorrectGroupOrder //@phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty +//@phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps final class SimpleBean1 extends \CoolBeans\Bean { private int $col1;