From 148a8994211a1d9f30d498b45278fe082fb2747a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= <me@jiripudil.cz>
Date: Mon, 30 Nov 2020 12:19:26 +0100
Subject: [PATCH] support PHP 8

---
 .gitlab-ci.yml                                |  8 ++-----
 composer.json                                 |  6 ++---
 src/Row.php                                   |  3 +--
 src/Scaffolding/Column.php                    | 21 +++++-----------
 src/Scaffolding/PostgresReflector.php         | 10 +++-----
 .../PrivateConstructorDecorator.php           |  2 +-
 .../ReconstituteConstructorDecorator.php      |  4 ++--
 src/Scaffolding/TableDecorator.php            | 24 +++++++------------
 src/TableManager.php                          | 20 ++++------------
 src/TypeMapper.php                            | 22 ++++-------------
 src/exceptions.php                            |  5 +---
 11 files changed, 36 insertions(+), 89 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f3de909..61aa246 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,8 +1,9 @@
 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
diff --git a/composer.json b/composer.json
index 5676bfc..23abd38 100644
--- a/composer.json
+++ b/composer.json
@@ -22,14 +22,14 @@
 
 
     "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.3",
+        "grifart/phpstan-oneline": "^0.4.0",
         "phpstan/phpstan": "^0.12"
 
     },
diff --git a/src/Row.php b/src/Row.php
index 52088e5..0db2c77 100644
--- a/src/Row.php
+++ b/src/Row.php
@@ -9,8 +9,7 @@ interface Row
 
 	/**
 	 * @param mixed[] $values
-	 * @return self
 	 */
-	public static function reconstitute(array $values);
+	public static function reconstitute(array $values): static;
 
 }
diff --git a/src/Scaffolding/Column.php b/src/Scaffolding/Column.php
index 34086dd..6d1cf63 100644
--- a/src/Scaffolding/Column.php
+++ b/src/Scaffolding/Column.php
@@ -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
 	{
diff --git a/src/Scaffolding/PostgresReflector.php b/src/Scaffolding/PostgresReflector.php
index 871dc7d..a580c8d 100644
--- a/src/Scaffolding/PostgresReflector.php
+++ b/src/Scaffolding/PostgresReflector.php
@@ -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
diff --git a/src/Scaffolding/PrivateConstructorDecorator.php b/src/Scaffolding/PrivateConstructorDecorator.php
index 0007998..0889283 100644
--- a/src/Scaffolding/PrivateConstructorDecorator.php
+++ b/src/Scaffolding/PrivateConstructorDecorator.php
@@ -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();
 	}
 }
diff --git a/src/Scaffolding/ReconstituteConstructorDecorator.php b/src/Scaffolding/ReconstituteConstructorDecorator.php
index 046c3cf..e2739f9 100644
--- a/src/Scaffolding/ReconstituteConstructorDecorator.php
+++ b/src/Scaffolding/ReconstituteConstructorDecorator.php
@@ -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));
 	}
 }
diff --git a/src/Scaffolding/TableDecorator.php b/src/Scaffolding/TableDecorator.php
index f5c412f..61b8328 100644
--- a/src/Scaffolding/TableDecorator.php
+++ b/src/Scaffolding/TableDecorator.php
@@ -18,26 +18,21 @@ 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
@@ -270,10 +265,7 @@ final class TableDecorator implements ClassDecorator
 		$this->implementConfigMethod($classType, $name, new Code\PhpLiteral($namespace->unresolveName($class) . '::class'));
 	}
 
-	/**
-	 * @param mixed $value
-	 */
-	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()
diff --git a/src/TableManager.php b/src/TableManager.php
index 1bc7118..7a921cd 100644
--- a/src/TableManager.php
+++ b/src/TableManager.php
@@ -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
diff --git a/src/TypeMapper.php b/src/TypeMapper.php
index 3ded68d..310d607 100644
--- a/src/TypeMapper.php
+++ b/src/TypeMapper.php
@@ -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 = [];
 
 
 	/**
@@ -29,11 +29,7 @@ final class TypeMapper
 		$this->mappings[] = $mapper;
 	}
 
-	/**
-	 * @param mixed $value
-	 * @return mixed
-	 */
-	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?
 		}
@@ -48,17 +44,7 @@ final class TypeMapper
 		throw CouldNotMapTypeException::didYouRegisterTypeMapperFor($typeName, $value);
 	}
 
-	/**
-	 * @param mixed $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) {
diff --git a/src/exceptions.php b/src/exceptions.php
index e1e80eb..405d957 100644
--- a/src/exceptions.php
+++ b/src/exceptions.php
@@ -21,10 +21,7 @@ final class ProbablyBrokenPrimaryIndexImplementation extends UsageException {
 final class CouldNotMapTypeException extends UsageException
 {
 
-	/**
-	 * @param mixed $value
-	 */
-	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 "
-- 
GitLab