Skip to content

Commit 08e1faa

Browse files
authored
Merge pull request #52 from keboola/zajca-types
Fill with types
2 parents 9b3a389 + b50e5da commit 08e1faa

27 files changed

Lines changed: 1455 additions & 1338 deletions

bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
date_default_timezone_set('Europe/Prague');
4-
ini_set('display_errors', true);
6+
ini_set('display_errors', 'true');
57
error_reporting(E_ALL);
68

79
require_once 'vendor/autoload.php';

composer.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.4"
12+
"php": "^7.4|^8"
1313
},
1414
"require-dev": {
1515
"phpunit/phpunit": "^9",
1616
"squizlabs/php_codesniffer": "^3",
1717
"phpstan/phpstan": "^1",
18-
"php-parallel-lint/php-parallel-lint": "^1"
18+
"php-parallel-lint/php-parallel-lint": "^1",
19+
"keboola/coding-standard": "^13.0"
1920
},
2021
"autoload": {
2122
"psr-4": {
@@ -29,8 +30,9 @@
2930
},
3031
"scripts": {
3132
"tests": "phpunit",
32-
"phpstan": "phpstan analyse ./src --no-progress --level=max -c phpstan.neon",
33-
"phpcs": "phpcs -n --standard=psr2 --ignore=vendor --extensions=php .",
33+
"phpstan": "phpstan analyse ./src ./tests --no-progress --level=max -c phpstan.neon",
34+
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
35+
"phpcs-report": "phpcs -n --report=source -s --ignore=vendor --extensions=php .",
3436
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
3537
"phplint": "parallel-lint -j 10 --exclude vendor .",
3638
"build": [
@@ -43,5 +45,13 @@
4345
"@composer validate --no-check-all --strict",
4446
"@build"
4547
]
48+
},
49+
"config": {
50+
"sort-packages": true,
51+
"optimize-autoloader": true,
52+
"lock": false,
53+
"allow-plugins": {
54+
"dealerdirect/phpcodesniffer-composer-installer": true
55+
}
4656
}
4757
}

phpcs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Project">
3+
<rule ref="vendor/keboola/coding-standard/src/ruleset.xml"/>
4+
</ruleset>

phpstan.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
parameters:
2-
checkMissingIterableValueType: false
2+
checkMissingIterableValueType: false
3+
ignoreErrors:
4+
- '#^Parameter \#. \$options of class Keboola\\Datatype\\.* constructor expects array\{length\?\: string\|null, nullable\?\: bool, default\?\: string\|null.*}, .*array.* given\.$#'
5+
- '#^Parameter \#. \$options of class Keboola\\Datatype\\.* constructor expects array\{length\?\: array\|string\|null, nullable\?\: bool, default\?\: string\|null.*}, .*array.* given\.$#'

src/Definition/BaseType.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Keboola\Datatype\Definition;
46

57
class BaseType
68
{
7-
const BOOLEAN = "BOOLEAN";
8-
const DATE = "DATE";
9-
const FLOAT = "FLOAT";
10-
const INTEGER = "INTEGER";
11-
const NUMERIC = "NUMERIC";
12-
const STRING = "STRING";
13-
const TIMESTAMP = "TIMESTAMP";
9+
public const BOOLEAN = 'BOOLEAN';
10+
public const DATE = 'DATE';
11+
public const FLOAT = 'FLOAT';
12+
public const INTEGER = 'INTEGER';
13+
public const NUMERIC = 'NUMERIC';
14+
public const STRING = 'STRING';
15+
public const TIMESTAMP = 'TIMESTAMP';
1416
}

src/Definition/Common.php

Lines changed: 51 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Keboola\Datatype\Definition;
46

57
abstract class Common implements DefinitionInterface
68
{
7-
const KBC_METADATA_KEY_TYPE = "KBC.datatype.type";
8-
const KBC_METADATA_KEY_NULLABLE = "KBC.datatype.nullable";
9-
const KBC_METADATA_KEY_BASETYPE = "KBC.datatype.basetype";
10-
const KBC_METADATA_KEY_LENGTH = "KBC.datatype.length";
11-
const KBC_METADATA_KEY_DEFAULT = "KBC.datatype.default";
9+
public const KBC_METADATA_KEY_TYPE = 'KBC.datatype.type';
10+
public const KBC_METADATA_KEY_NULLABLE = 'KBC.datatype.nullable';
11+
public const KBC_METADATA_KEY_BASETYPE = 'KBC.datatype.basetype';
12+
public const KBC_METADATA_KEY_LENGTH = 'KBC.datatype.length';
13+
public const KBC_METADATA_KEY_DEFAULT = 'KBC.datatype.default';
1214

13-
const KBC_METADATA_KEY_COMPRESSION = "KBC.datatype.compression";
14-
const KBC_METADATA_KEY_FORMAT = "KBC.datatype.format";
15+
public const KBC_METADATA_KEY_COMPRESSION = 'KBC.datatype.compression';
16+
public const KBC_METADATA_KEY_FORMAT = 'KBC.datatype.format';
1517

16-
/**
17-
* @var string
18-
*/
19-
protected $type;
18+
protected string $type;
2019

21-
/**
22-
* @var string
23-
*/
24-
protected $length = null;
20+
protected ?string $length = null;
2521

26-
/**
27-
* @var bool
28-
*/
29-
protected $nullable = true;
22+
protected bool $nullable = true;
3023

31-
/**
32-
* @var string
33-
*/
34-
protected $default = null;
24+
protected ?string $default = null;
3525

3626
/**
3727
* Common constructor.
3828
*
39-
* @param string $type
40-
* @param array $options -- length, nullable, default
29+
* @param array{length?:string|null, nullable?:bool, default?:string|null} $options
4130
*/
42-
public function __construct($type, $options = [])
31+
public function __construct(string $type, array $options = [])
4332
{
4433
$this->type = $type;
4534
if (isset($options['length'])) {
@@ -53,103 +42,86 @@ public function __construct($type, $options = [])
5342
}
5443
}
5544

56-
/**
57-
* @return string
58-
*/
59-
public function getType()
45+
public function getType(): string
6046
{
6147
return $this->type;
6248
}
6349

64-
/**
65-
* @return string|null
66-
*/
67-
public function getLength()
50+
public function getLength(): ?string
6851
{
6952
return $this->length;
7053
}
7154

72-
/**
73-
* @return boolean
74-
*/
75-
public function isNullable()
55+
public function isNullable(): bool
7656
{
7757
return $this->nullable;
7858
}
7959

80-
/**
81-
* @return string|null
82-
*/
83-
public function getDefault()
60+
public function getDefault(): ?string
8461
{
8562
return $this->default;
8663
}
8764

88-
/**
89-
* @return string
90-
*/
91-
abstract public function getSQLDefinition();
65+
abstract public function getSQLDefinition(): string;
9266

93-
/**
94-
* @return string
95-
*/
96-
abstract public function getBasetype();
67+
abstract public function getBasetype(): string;
9768

9869
/**
99-
* @return array
70+
* @return array<mixed>
10071
*/
101-
abstract public function toArray();
72+
abstract public function toArray(): array;
10273

10374
/**
10475
* @param string|int|null $length
105-
* @return bool
10676
*/
107-
protected function isEmpty($length)
77+
//phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
78+
protected function isEmpty($length): bool
10879
{
10980
return $length === null || $length === '';
11081
}
11182

11283
/**
113-
* @return array
84+
* @return array<int, array{key:string,value:mixed}>
11485
*/
115-
public function toMetadata()
86+
public function toMetadata(): array
11687
{
11788
$metadata = [
11889
[
119-
"key" => self::KBC_METADATA_KEY_TYPE,
120-
"value" => $this->getType(),
90+
'key' => self::KBC_METADATA_KEY_TYPE,
91+
'value' => $this->getType(),
12192
],[
122-
"key" => self::KBC_METADATA_KEY_NULLABLE,
123-
"value" => $this->isNullable()
93+
'key' => self::KBC_METADATA_KEY_NULLABLE,
94+
'value' => $this->isNullable(),
12495
],[
125-
"key" => self::KBC_METADATA_KEY_BASETYPE,
126-
"value" => $this->getBasetype()
127-
]
96+
'key' => self::KBC_METADATA_KEY_BASETYPE,
97+
'value' => $this->getBasetype(),
98+
],
12899
];
129100
if ($this->getLength()) {
130101
$metadata[] = [
131-
"key" => self::KBC_METADATA_KEY_LENGTH,
132-
"value" => $this->getLength()
102+
'key' => self::KBC_METADATA_KEY_LENGTH,
103+
'value' => $this->getLength(),
133104
];
134105
}
135106
if (!is_null($this->getDefault())) {
136107
$metadata[] = [
137-
"key" => self::KBC_METADATA_KEY_DEFAULT,
138-
"value" => $this->getDefault(),
108+
'key' => self::KBC_METADATA_KEY_DEFAULT,
109+
'value' => $this->getDefault(),
139110
];
140111
}
141112
return $metadata;
142113
}
143114

144115
/**
145-
* @param string|null $length
146-
* @param int $firstMax
147-
* @param int $secondMax
148-
* @param bool $firstMustBeBigger
149-
* @return bool
116+
* @param null|int|string $length
150117
*/
151-
protected function validateNumericLength($length, $firstMax, $secondMax, $firstMustBeBigger = true)
152-
{
118+
//phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
119+
protected function validateNumericLength(
120+
$length,
121+
int $firstMax,
122+
int $secondMax,
123+
bool $firstMustBeBigger = true
124+
): bool {
153125
if ($this->isEmpty($length)) {
154126
return true;
155127
}
@@ -169,20 +141,14 @@ protected function validateNumericLength($length, $firstMax, $secondMax, $firstM
169141
if (isset($parts[1]) && ((int) $parts[1] > $secondMax)) {
170142
return false;
171143
}
172-
173-
if ($firstMustBeBigger && isset($parts[1]) && (int) $parts[1] > (int) $parts[0]) {
174-
return false;
175-
}
176-
return true;
144+
return !($firstMustBeBigger && isset($parts[1]) && (int) $parts[1] > (int) $parts[0]);
177145
}
178146

179147
/**
180148
* @param string|int|null $length
181-
* @param int $max
182-
* @param int $min
183-
* @return bool
184149
*/
185-
protected function validateMaxLength($length, $max, $min = 1)
150+
//phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
151+
protected function validateMaxLength($length, int $max, int $min = 1): bool
186152
{
187153
if ($this->isEmpty($length)) {
188154
return true;
@@ -191,9 +157,9 @@ protected function validateMaxLength($length, $max, $min = 1)
191157
if (!is_numeric($length)) {
192158
return false;
193159
}
194-
if ((int) $length < $min || (int) $length > $max) {
160+
if (filter_var($length, FILTER_VALIDATE_INT) === false) {
195161
return false;
196162
}
197-
return true;
163+
return (int) $length >= $min && (int) $length <= $max;
198164
}
199165
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Keboola\Datatype\Definition;
46

57
interface DefinitionInterface
68
{
7-
/**
8-
* @return string
9-
*/
10-
public function getSQLDefinition();
9+
public function getSQLDefinition(): string;
1110

1211
/**
1312
* @return array{type:string, length:string|null, nullable:bool, compression?:mixed}
1413
*/
15-
public function toArray();
14+
public function toArray(): array;
1615

17-
/**
18-
* @return string
19-
*/
20-
public function getBasetype();
16+
public function getBasetype(): string;
2117
}

0 commit comments

Comments
 (0)