Skip to content

Commit

Permalink
Merge pull request #22 from vossik/codestyle
Browse files Browse the repository at this point in the history
Codestyle
  • Loading branch information
peldax authored Jun 1, 2021
2 parents 329ac2e + 90c7006 commit 0dd385b
Show file tree
Hide file tree
Showing 31 changed files with 103 additions and 135 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/ClassUniqueConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/Attribute/DefaultValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
final class DefaultValue
{
public function __construct(
public string $defaultValue
) {}
public string $defaultValue,
)
{
}
}
2 changes: 1 addition & 1 deletion src/Attribute/TypeOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class TypeOverride

public function __construct(
public string $type,
int ...$lengthArgs
int ...$lengthArgs,
)
{
$this->lengthArgs = $lengthArgs;
Expand Down
16 changes: 7 additions & 9 deletions src/Bean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -84,7 +82,7 @@ public function offsetExists($offset) : bool
$property = $this->reflection->getProperty($offset);

return $property->isPublic();
} catch (\ReflectionException $e) {
} catch (\ReflectionException) {
return false;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Bridge/Nette/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 17 additions & 3 deletions src/Bridge/Nette/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@

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();
}

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
Expand Down
4 changes: 1 addition & 3 deletions src/Bridge/Nette/TDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Command/SqlGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -315,7 +314,6 @@ private function getBeans(string $destination) : array
throw new \CoolBeans\Exception\BeanWithoutPublicProperty('Bean ' . $bean->getShortName() . ' has no public property.');
}


$beans[] = $class;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Contract/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ContextFactory
{
public function create() : \Nette\Database\Context;
public function create() : \Nette\Database\Explorer;
}
2 changes: 1 addition & 1 deletion src/Contract/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/Contract/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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;

public function rewind() : void;

public function valid() : bool;

public function key(); // : string|int
public function key() : string|int;

public function current() : ?\CoolBeans\Contract\Row;

Expand Down
4 changes: 1 addition & 3 deletions src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions src/Decorator/Active.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace CoolBeans\Decorator;

use \CoolBeans\Contract\PrimaryKey;

final class Active implements \CoolBeans\Contract\DataSource
{
use \Nette\SmartObject;
Expand All @@ -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);

Expand All @@ -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]);

Expand Down
18 changes: 6 additions & 12 deletions src/Decorator/Bean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
19 changes: 5 additions & 14 deletions src/Decorator/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@

namespace CoolBeans\Decorator;

use \CoolBeans\Contract\PrimaryKey;

final class History implements \CoolBeans\Contract\DataSource
{
use \Nette\SmartObject;
use \CoolBeans\Decorator\TDecorator;

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);
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 1 addition & 3 deletions src/Decorator/TDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 6 additions & 5 deletions src/PrimaryKey/IntPrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
Loading

0 comments on commit 0dd385b

Please sign in to comment.