Skip to content
Snippets Groups Projects
Commit 2800124f authored by Jan Kuchař's avatar Jan Kuchař
Browse files

Merge branch 'php8' into 'master'

PHP 8

See merge request !7
parents fc31b8d8 ab40391d
No related branches found
No related tags found
1 merge request!7PHP 8
Pipeline #30657 passed
stages:
- test
.test-verify-template: &test-verify-template
test-verify:
stage: test
image: grifart/php8.0-with-all-modules-and-various-tools
interruptible: true
before_script:
......@@ -10,8 +11,3 @@ stages:
script:
- composer run verify
test.verify.php74:
<<: *test-verify-template
image: grifart/php7.4-with-gulp-and-all-php-modules
......@@ -22,15 +22,15 @@
"require": {
"php": "^7.4",
"php": "^8.0",
"dibi/dibi": "^4.0.2",
"grifart/class-scaffolder": "^0.2.0|^0.3.0",
"grifart/class-scaffolder": "^0.4.0",
"nette/utils": "^3.0.1"
},
"require-dev": {
"nette/tester": "^2.3",
"grifart/phpstan-oneline": "^0.2.2",
"phpstan/phpstan": "^0.11.17"
"grifart/phpstan-oneline": "^0.4.0",
"phpstan/phpstan": "^0.12"
},
......
......@@ -8,7 +8,10 @@ interface Modifications
{
/** @internal used by {@see AccountsTable} */
/**
* @internal used by {@see AccountsTable}
* @return mixed[]
*/
public function getModifications(): array;
/** @return null|PrimaryKey if null it means, that row is new (do INSERT) */
......
......@@ -7,7 +7,7 @@ interface PrimaryKey
{
/**
* @return array query used in WHERE to narrow down results into one record
* @return array<string, mixed> query used in WHERE to narrow down results into one record
*/
public function getQuery(): array;
......
......@@ -7,7 +7,9 @@ namespace Grifart\Tables;
interface Row
{
/** @return self */
public static function reconstitute(array $values);
/**
* @param mixed[] $values
*/
public static function reconstitute(array $values): static;
}
......@@ -7,21 +7,12 @@ namespace Grifart\Tables\Scaffolding;
final class Column
{
private string $name;
private string $type;
private bool $nullable;
private bool $hasDefaultValue;
public function __construct(string $name, string $type, bool $nullable, bool $hasDefaultValue)
{
$this->name = $name;
$this->type = $type;
$this->nullable = $nullable;
$this->hasDefaultValue = $hasDefaultValue;
}
public function __construct(
private string $name,
private string $type,
private bool $nullable,
private bool $hasDefaultValue,
) {}
public function getName(): string
{
......
......@@ -9,13 +9,9 @@ use Dibi\Connection;
final class PostgresReflector
{
/** @var Connection */
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function __construct(
private Connection $connection
) {}
/**
* source: https://stackoverflow.com/a/51897900/631369
......
......@@ -11,6 +11,6 @@ final class PrivateConstructorDecorator implements ClassDecorator
public function decorate(ClassType $classType, ClassDefinition $definition): void
{
$classType->getMethod('__construct')->setVisibility('private');
$classType->getMethod('__construct')->setPrivate();
}
}
......@@ -14,7 +14,7 @@ final class ReconstituteConstructorDecorator implements ClassDecorator
public function decorate(ClassType $classType, ClassDefinition $definition): void
{
$reconstitute = $classType->addMethod('reconstitute')
->setReturnType('self')
->setReturnType('static')
->setParameters([(new Code\Parameter('values'))->setTypeHint('array')])
->setStatic();
......@@ -25,6 +25,6 @@ final class ReconstituteConstructorDecorator implements ClassDecorator
// todo: add array_keys that it contains just keys that are necessary
$reconstitute->addBody("return new self($questionMarks);", \array_values($literals));
$reconstitute->addBody("return new static($questionMarks);", \array_values($literals));
}
}
......@@ -8,8 +8,8 @@ namespace Grifart\Tables\Scaffolding;
use Grifart\ClassScaffolder\Decorators\GettersDecorator;
use Grifart\ClassScaffolder\Decorators\InitializingConstructorDecorator;
use Grifart\ClassScaffolder\Decorators\PropertiesDecorator;
use Grifart\ClassScaffolder\Definition\ClassDefinition;
use Grifart\ClassScaffolder\Definition\ClassDefinitionBuilder;
use Grifart\ClassScaffolder\Definition\Types\Type;
use Grifart\Tables\Row;
use Grifart\Tables\TypeMapper;
use function Grifart\ClassScaffolder\Definition\Types\nullable;
......@@ -23,6 +23,9 @@ final class Scaffolding
return "$schema.$table.$column";
}
/**
* @return ClassDefinition[]
*/
public static function definitionsForPgTable(
PostgresReflector $pgReflector,
TypeMapper $mapper,
......
......@@ -18,28 +18,24 @@ use Webmozart\Assert\Assert;
final class TableDecorator implements ClassDecorator
{
/** @var string */
private $schema;
private string $schema;
/** @var string */
private $tableName;
private string $tableName;
/** @var string */
private $primaryKeyClass;
private string $primaryKeyClass;
/** @var string */
private $rowClass;
private string $rowClass;
/** @var string */
private $modificationClass;
private string $modificationClass;
/** @var Column[] */
private $columnInfo;
private array $columnInfo;
/** @var array<string, Type> */
private $columnPhpTypes;
private array $columnPhpTypes;
/**
* @param array<string, Column> $columnInfo
* @param array<string, Type> $columnPhpTypes
*/
public function __construct(string $schema, string $tableName, string $primaryKeyClass, string $rowClass, string $modificationClass, array $columnInfo, array $columnPhpTypes)
......@@ -269,7 +265,7 @@ final class TableDecorator implements ClassDecorator
$this->implementConfigMethod($classType, $name, new Code\PhpLiteral($namespace->unresolveName($class) . '::class'));
}
private function implementConfigMethod(Code\ClassType $classType, string $name, $value): void
private function implementConfigMethod(Code\ClassType $classType, string $name, mixed $value): void
{
$classType->addMethod($name)
->setStatic()
......
......@@ -15,21 +15,11 @@ use Dibi\Connection;
final class TableManager
{
/** @var Connection */
private $connection;
/** @var TypeMapper */
private $phpToDatabaseMapper;
/** @var TypeMapper */
private $databaseToPhpMapper;
public function __construct(Connection $connection, TypeMapper $phpToDatabaseMapper, TypeMapper $databaseToPhpMapper)
{
$this->connection = $connection;
$this->phpToDatabaseMapper = $phpToDatabaseMapper;
$this->databaseToPhpMapper = $databaseToPhpMapper;
}
public function __construct(
private Connection $connection,
private TypeMapper $phpToDatabaseMapper,
private TypeMapper $databaseToPhpMapper,
) {}
public function insert(Table $table, Modifications $changes): void
......@@ -55,7 +45,13 @@ final class TableManager
return NULL;
}
/** @return Row[] (subclass of row) */
/**
* @param array<string, mixed> $conditions Conditions provides low-level access to "where" clause concatenated by %and.
* More: https://dibiphp.com/en/documentation
* Note! Types and names are currently NOT mapped.
* Please follow https://gitlab.grifart.cz/grifart/tables/-/issues/2 on progress.
* @return Row[] (subclass of row)
*/
public function findBy(Table $table, array $conditions): array
{
$result = $this->connection->query(
......@@ -114,6 +110,10 @@ final class TableManager
}
/**
* @param array<string, mixed> $values
* @return array<string, mixed>
*/
private static function mapTypes(TypeMapper $mapper, array $values, Table $table): array
{
// could not use array_map as it does not preserve indexes
......
......@@ -10,12 +10,12 @@ final class TypeMapper
/**
* @var (callable(string $typeName, string $location): (string|Type|null))[]
*/
private $matchers = [];
private array $matchers = [];
/**
* @var (callable(mixed $value, string $typeName): mixed)[]
*/
private $mappings = [];
private array $mappings = [];
/**
......@@ -24,12 +24,12 @@ final class TypeMapper
*
* TODO: isn't it too general? Or just map db-type name to php type and back?
*/
public function addMapping(callable $typeMatcher, callable $mapper) {
public function addMapping(callable $typeMatcher, callable $mapper): void {
$this->matchers[] = $typeMatcher;
$this->mappings[] = $mapper;
}
public function map(string $location, string $typeName, $value) {
public function map(string $location, string $typeName, mixed $value): mixed {
if ($value === NULL) {
return NULL; // todo: really do not translate?
}
......@@ -44,14 +44,7 @@ final class TypeMapper
throw CouldNotMapTypeException::didYouRegisterTypeMapperFor($typeName, $value);
}
private function getTypeForValue($value): string {
return !\is_object($value) ? \gettype($value) : \get_class($value);
}
/**
* @return string|Type
*/
public function mapType(string $location, string $typeName)
public function mapType(string $location, string $typeName): string|Type
{
foreach($this->matchers as $idx => $matcher) {
if ( ($translatingType = $matcher($typeName, $location)) !== NULL) {
......
......@@ -21,7 +21,7 @@ final class ProbablyBrokenPrimaryIndexImplementation extends UsageException {
final class CouldNotMapTypeException extends UsageException
{
public static function didYouRegisterTypeMapperFor(string $typeName, $value): self
public static function didYouRegisterTypeMapperFor(string $typeName, mixed $value): self
{
return new self(
"Did you register type mapper for type '$typeName' and value of type "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment