Skip to content

Commit

Permalink
Code: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 13, 2023
1 parent adeb39d commit 9964566
Show file tree
Hide file tree
Showing 26 changed files with 258 additions and 311 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ parameters:
- .docs

ignoreErrors:
- '#Construct empty\(\) is not allowed. Use more strict comparison\.#'
8 changes: 3 additions & 5 deletions src/Analyser/Database/DatabaseAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
class DatabaseAnalyser implements IAnalyser
{

/** @var Connection */
private $connection;
private Connection $connection;

/** @var Driver */
private $driver;
private Driver $driver;

public function __construct(string $dns, string $username, ?string $password = null)
{
Expand Down Expand Up @@ -69,7 +67,7 @@ protected function analyseColumns(Table $table): void
$column->setNullable($col['nullable']);
$column->setType(Helpers::columnType($col['nativetype']));
$column->setDefault($col['default']);
$column->setOnUpdate(Strings::contains($col['vendor']['extra'] ?? $col['vendor']['Extra'], 'on update'));
$column->setOnUpdate(str_contains($col['vendor']['extra'] ?? $col['vendor']['Extra'], 'on update'));

// Analyse ENUM
if ($col['nativetype'] === ColumnTypes::NATIVE_TYPE_ENUM) {
Expand Down
67 changes: 28 additions & 39 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Config implements ArrayAccess
public const STRATEGY_SEPARATE = 2;

/** @var mixed[] */
protected $defaults = [
protected array $defaults = [
// Output folder
'output' => null,
// 1 => Entity + Repository + Mapper + Facade => same folder (folder = table name)
Expand Down Expand Up @@ -80,59 +80,45 @@ class Config implements ArrayAccess
];

/** @var mixed[] */
protected $config = [];
protected array $config = [];

/**
* @param mixed[] $configuration
*/
public function __construct(array $configuration)
{
// Validate config
if ($extra = array_diff_key($configuration, $this->defaults)) {
if (($extra = array_diff_key($configuration, $this->defaults)) !== []) {
$extra = implode(', ', array_keys($extra));

throw new InvalidStateException('Unknown configuration option ' . $extra . '.');
}

$this->config = array_merge($this->defaults, $configuration);
}

/**
* MAGIC METHODS ***********************************************************
*/

/**
* @return mixed
*/
public function __get(string $name)
public function get(string $name): mixed
{
return $this->offsetGet($name);
}

/**
* @param string|mixed $name
* @param mixed $value
*/
public function __set($name, $value): void
public function getString(string $name): string
{
$this->offsetSet($name, $value);
$ret = $this->offsetGet($name);
assert(is_string($ret));

return $ret;
}

/**
* @return mixed
*/
public function get(string $name)
public function getBool(string $name): bool
{
return $this->offsetGet($name);
}
$ret = $this->offsetGet($name);
assert(is_bool($ret));

/**
* ARRAY ACCESS ************************************************************
*/
return $ret;
}

/**
* @param mixed $offset
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->config);
}
Expand All @@ -149,21 +135,24 @@ public function offsetGet(mixed $offset): mixed
throw new InvalidStateException('Undefined offset: ' . $offset);
}

/**
* @param string|mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
$this->config[$offset] = $value;
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
unset($this->config[$offset]);
}

public function __get(string $name): mixed
{
return $this->offsetGet($name);
}

public function __set(string $name, ?string $value): void
{
$this->offsetSet($name, $value);
}

}
2 changes: 1 addition & 1 deletion src/Config/Impl/SeparateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SeparateConfig extends Config
{

/** @var mixed[] */
protected $defaults = [
protected array $defaults = [
// Output folder
'output' => null,
// Generator
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Impl/TogetherConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TogetherConfig extends Config
{

/** @var mixed[] */
protected $defaults = [
protected array $defaults = [
// Output folder
'output' => null,
// Generator
Expand Down
88 changes: 35 additions & 53 deletions src/Entity/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,36 @@
class Column
{

/** @var Table|null */
private $table;
private ?Table $table = null;

/** @var string */
private $name;
private ?string $name = null;

/** @var mixed */
private $type;
private ?string $type = null;

/** @var mixed */
private $subtype;
private ?string $subtype = null;

/** @var bool */
private $nullable;
private ?bool $nullable = null;

/** @var mixed */
private $default;
private ?string $default = null;

/** @var string[] */
private $enum = [];
private array $enum = [];

/** @var bool */
private $onUpdate;
private bool $onUpdate;

/** @var PhpDoc|null */
private $phpDoc;
private ?PhpDoc $phpDoc = null;

/** @var bool|null */
private $primary;
private ?bool $primary = null;

/** @var bool */
private $unique;
private bool $unique;

/** @var bool */
private $index;
private bool $index;

/** @var ForeignKey */
private $foreignKey;
private ForeignKey $foreignKey;

public function attach(Table $table): void
{
if ($this->table) {
if ($this->table !== null) {
throw new InvalidAttachException('Column is already attached to table.');
}

Expand All @@ -58,7 +46,7 @@ public function attach(Table $table): void

public function getTable(): Table
{
if (!$this->table) {
if ($this->table === null) {
throw new LogicException('Table is needed');
}

Expand All @@ -67,6 +55,10 @@ public function getTable(): Table

public function getName(): string
{
if ($this->name === null) {
throw new LogicException('Name is needed');
}

return $this->name;
}

Expand All @@ -75,39 +67,35 @@ public function setName(string $name): void
$this->name = $name;
}

/**
* @return mixed
*/
public function getType()
public function getType(): string
{
if ($this->type === null) {
throw new LogicException('Type is needed');
}

return $this->type;
}

/**
* @param mixed $type
*/
public function setType($type): void
public function setType(string $type): void
{
$this->type = $type;
}

/**
* @return mixed
*/
public function getSubtype()
public function getSubtype(): string
{
if ($this->subtype === null) {
throw new LogicException('Subtype is needed');
}

return $this->subtype;
}

/**
* @param mixed $subtype
*/
public function setSubtype($subtype): void
public function setSubtype(string $subtype): void
{
$this->subtype = $subtype;
}

public function isNullable(): bool
public function isNullable(): ?bool
{
return $this->nullable;
}
Expand All @@ -117,18 +105,12 @@ public function setNullable(bool $nullable): void
$this->nullable = $nullable;
}

/**
* @return mixed
*/
public function getDefault()
public function getDefault(): ?string
{
return $this->default;
}

/**
* @param mixed $default
*/
public function setDefault($default): void
public function setDefault(string $default): void
{
$this->default = $default;
}
Expand Down Expand Up @@ -161,7 +143,7 @@ public function setOnUpdate(bool $onUpdate): void

public function getPhpDoc(): PhpDoc
{
if (!$this->phpDoc) {
if ($this->phpDoc === null) {
$this->phpDoc = new PhpDoc();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Database
{

/** @var Table[] */
private $tables = [];
private array $tables = [];

/**
* @return Table[]
Expand Down
12 changes: 4 additions & 8 deletions src/Entity/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
class ForeignKey
{

/** @var string */
private $sourceTable;
private string $sourceTable;

/** @var string */
private $sourceColumn;
private string $sourceColumn;

/** @var string */
private $referenceTable;
private string $referenceTable;

/** @var string */
private $referenceColumn;
private string $referenceColumn;

public function getSourceTable(): string
{
Expand Down
Loading

0 comments on commit 9964566

Please sign in to comment.