Skip to content

Commit 95c5b80

Browse files
committed
Code: add types
1 parent 01e15b9 commit 95c5b80

26 files changed

+258
-311
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ parameters:
1616
- .docs
1717

1818
ignoreErrors:
19+
- '#Construct empty\(\) is not allowed. Use more strict comparison\.#'

src/Analyser/Database/DatabaseAnalyser.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
class DatabaseAnalyser implements IAnalyser
1717
{
1818

19-
/** @var Connection */
20-
private $connection;
19+
private Connection $connection;
2120

22-
/** @var Driver */
23-
private $driver;
21+
private Driver $driver;
2422

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

7472
// Analyse ENUM
7573
if ($col['nativetype'] === ColumnTypes::NATIVE_TYPE_ENUM) {

src/Config/Config.php

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Config implements ArrayAccess
1818
public const STRATEGY_SEPARATE = 2;
1919

2020
/** @var mixed[] */
21-
protected $defaults = [
21+
protected array $defaults = [
2222
// Output folder
2323
'output' => null,
2424
// 1 => Entity + Repository + Mapper + Facade => same folder (folder = table name)
@@ -80,59 +80,45 @@ class Config implements ArrayAccess
8080
];
8181

8282
/** @var mixed[] */
83-
protected $config = [];
83+
protected array $config = [];
8484

8585
/**
8686
* @param mixed[] $configuration
8787
*/
8888
public function __construct(array $configuration)
8989
{
9090
// Validate config
91-
if ($extra = array_diff_key($configuration, $this->defaults)) {
91+
if (($extra = array_diff_key($configuration, $this->defaults)) !== []) {
9292
$extra = implode(', ', array_keys($extra));
93+
9394
throw new InvalidStateException('Unknown configuration option ' . $extra . '.');
9495
}
9596

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

99-
/**
100-
* MAGIC METHODS ***********************************************************
101-
*/
102-
103-
/**
104-
* @return mixed
105-
*/
106-
public function __get(string $name)
100+
public function get(string $name): mixed
107101
{
108102
return $this->offsetGet($name);
109103
}
110104

111-
/**
112-
* @param string|mixed $name
113-
* @param mixed $value
114-
*/
115-
public function __set($name, $value): void
105+
public function getString(string $name): string
116106
{
117-
$this->offsetSet($name, $value);
107+
$ret = $this->offsetGet($name);
108+
assert(is_string($ret));
109+
110+
return $ret;
118111
}
119112

120-
/**
121-
* @return mixed
122-
*/
123-
public function get(string $name)
113+
public function getBool(string $name): bool
124114
{
125-
return $this->offsetGet($name);
126-
}
115+
$ret = $this->offsetGet($name);
116+
assert(is_bool($ret));
127117

128-
/**
129-
* ARRAY ACCESS ************************************************************
130-
*/
118+
return $ret;
119+
}
131120

132-
/**
133-
* @param mixed $offset
134-
*/
135-
public function offsetExists($offset): bool
121+
public function offsetExists(mixed $offset): bool
136122
{
137123
return array_key_exists($offset, $this->config);
138124
}
@@ -149,21 +135,24 @@ public function offsetGet(mixed $offset): mixed
149135
throw new InvalidStateException('Undefined offset: ' . $offset);
150136
}
151137

152-
/**
153-
* @param string|mixed $offset
154-
* @param mixed $value
155-
*/
156-
public function offsetSet($offset, $value): void
138+
public function offsetSet(mixed $offset, mixed $value): void
157139
{
158140
$this->config[$offset] = $value;
159141
}
160142

161-
/**
162-
* @param mixed $offset
163-
*/
164-
public function offsetUnset($offset): void
143+
public function offsetUnset(mixed $offset): void
165144
{
166145
unset($this->config[$offset]);
167146
}
168147

148+
public function __get(string $name): mixed
149+
{
150+
return $this->offsetGet($name);
151+
}
152+
153+
public function __set(string $name, ?string $value): void
154+
{
155+
$this->offsetSet($name, $value);
156+
}
157+
169158
}

src/Config/Impl/SeparateConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SeparateConfig extends Config
88
{
99

1010
/** @var mixed[] */
11-
protected $defaults = [
11+
protected array $defaults = [
1212
// Output folder
1313
'output' => null,
1414
// Generator

src/Config/Impl/TogetherConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TogetherConfig extends Config
88
{
99

1010
/** @var mixed[] */
11-
protected $defaults = [
11+
protected array $defaults = [
1212
// Output folder
1313
'output' => null,
1414
// Generator

src/Entity/Column.php

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,36 @@
88
class Column
99
{
1010

11-
/** @var Table|null */
12-
private $table;
11+
private ?Table $table = null;
1312

14-
/** @var string */
15-
private $name;
13+
private ?string $name = null;
1614

17-
/** @var mixed */
18-
private $type;
15+
private ?string $type = null;
1916

20-
/** @var mixed */
21-
private $subtype;
17+
private ?string $subtype = null;
2218

23-
/** @var bool */
24-
private $nullable;
19+
private ?bool $nullable = null;
2520

26-
/** @var mixed */
27-
private $default;
21+
private ?string $default = null;
2822

2923
/** @var string[] */
30-
private $enum = [];
24+
private array $enum = [];
3125

32-
/** @var bool */
33-
private $onUpdate;
26+
private bool $onUpdate;
3427

35-
/** @var PhpDoc|null */
36-
private $phpDoc;
28+
private ?PhpDoc $phpDoc = null;
3729

38-
/** @var bool|null */
39-
private $primary;
30+
private ?bool $primary = null;
4031

41-
/** @var bool */
42-
private $unique;
32+
private bool $unique;
4333

44-
/** @var bool */
45-
private $index;
34+
private bool $index;
4635

47-
/** @var ForeignKey */
48-
private $foreignKey;
36+
private ForeignKey $foreignKey;
4937

5038
public function attach(Table $table): void
5139
{
52-
if ($this->table) {
40+
if ($this->table !== null) {
5341
throw new InvalidAttachException('Column is already attached to table.');
5442
}
5543

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

5947
public function getTable(): Table
6048
{
61-
if (!$this->table) {
49+
if ($this->table === null) {
6250
throw new LogicException('Table is needed');
6351
}
6452

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

6856
public function getName(): string
6957
{
58+
if ($this->name === null) {
59+
throw new LogicException('Name is needed');
60+
}
61+
7062
return $this->name;
7163
}
7264

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

78-
/**
79-
* @return mixed
80-
*/
81-
public function getType()
70+
public function getType(): string
8271
{
72+
if ($this->type === null) {
73+
throw new LogicException('Type is needed');
74+
}
75+
8376
return $this->type;
8477
}
8578

86-
/**
87-
* @param mixed $type
88-
*/
89-
public function setType($type): void
79+
public function setType(string $type): void
9080
{
9181
$this->type = $type;
9282
}
9383

94-
/**
95-
* @return mixed
96-
*/
97-
public function getSubtype()
84+
public function getSubtype(): string
9885
{
86+
if ($this->subtype === null) {
87+
throw new LogicException('Subtype is needed');
88+
}
89+
9990
return $this->subtype;
10091
}
10192

102-
/**
103-
* @param mixed $subtype
104-
*/
105-
public function setSubtype($subtype): void
93+
public function setSubtype(string $subtype): void
10694
{
10795
$this->subtype = $subtype;
10896
}
10997

110-
public function isNullable(): bool
98+
public function isNullable(): ?bool
11199
{
112100
return $this->nullable;
113101
}
@@ -117,18 +105,12 @@ public function setNullable(bool $nullable): void
117105
$this->nullable = $nullable;
118106
}
119107

120-
/**
121-
* @return mixed
122-
*/
123-
public function getDefault()
108+
public function getDefault(): ?string
124109
{
125110
return $this->default;
126111
}
127112

128-
/**
129-
* @param mixed $default
130-
*/
131-
public function setDefault($default): void
113+
public function setDefault(string $default): void
132114
{
133115
$this->default = $default;
134116
}
@@ -161,7 +143,7 @@ public function setOnUpdate(bool $onUpdate): void
161143

162144
public function getPhpDoc(): PhpDoc
163145
{
164-
if (!$this->phpDoc) {
146+
if ($this->phpDoc === null) {
165147
$this->phpDoc = new PhpDoc();
166148
}
167149

src/Entity/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Database
88
{
99

1010
/** @var Table[] */
11-
private $tables = [];
11+
private array $tables = [];
1212

1313
/**
1414
* @return Table[]

src/Entity/ForeignKey.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
class ForeignKey
66
{
77

8-
/** @var string */
9-
private $sourceTable;
8+
private string $sourceTable;
109

11-
/** @var string */
12-
private $sourceColumn;
10+
private string $sourceColumn;
1311

14-
/** @var string */
15-
private $referenceTable;
12+
private string $referenceTable;
1613

17-
/** @var string */
18-
private $referenceColumn;
14+
private string $referenceColumn;
1915

2016
public function getSourceTable(): string
2117
{

0 commit comments

Comments
 (0)