Skip to content

Commit

Permalink
Adding class-string return types (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Sep 7, 2023
1 parent 92ccd4c commit db763c3
Show file tree
Hide file tree
Showing 31 changed files with 254 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
pull_request:
push:
branches:
- master
- '*.*'
- '*.*.*'

name: static analysis

jobs:
psalm:
uses: spiral/gh-actions/.github/workflows/psalm.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.2']
1 change: 0 additions & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ enabled:
- phpdoc_types_order
- print_to_echo
- regular_callable_call
- return_assignment
- self_accessor
- self_static_accessor
- set_type_to_cast
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require-dev": {
"phpunit/phpunit": "^9.5",
"spiral/tokenizer": "^2.8",
"vimeo/psalm": "^4.12"
"vimeo/psalm": "^5.12"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="." />
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
Expand Down
16 changes: 9 additions & 7 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function getSchema(): array
private function compute(Registry $registry, Entity $entity): void
{
$defaults = $registry->getDefaults();
$role = $entity->getRole();
\assert($role !== null);

$schema = [
Schema::ENTITY => $entity->getClass(),
Expand Down Expand Up @@ -127,7 +129,7 @@ private function compute(Registry $registry, Entity $entity): void
sprintf(
'Unable to apply schema modifier `%s` for the `%s` role. %s',
$modifier::class,
$entity->getRole(),
$role,
$e->getMessage()
),
(int)$e->getCode(),
Expand All @@ -143,8 +145,7 @@ private function compute(Registry $registry, Entity $entity): void

/** @var array<int, mixed> $schema */
ksort($schema);
$role = $entity->getRole();
assert(!empty($role));

$this->result[$role] = $schema;
}

Expand All @@ -170,10 +171,11 @@ private function renderColumns(Entity $entity): array
try {
$comparator->compare();
} catch (Throwable $e) {
throw new Exception\CompilerException(
sprintf("Error compiling the `%s` role.\n\n%s", $entity->getRole(), $e->getMessage()),
$e->getCode()
);
throw new Exception\CompilerException(sprintf(
"Error compiling the `%s` role.\n\n%s",
$entity->getRole() ?? 'unknown',
$e->getMessage()
), (int) $e->getCode());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;

/**
* @implements \ArrayAccess<int, mixed>
*/
final class Defaults implements \ArrayAccess
{
/**
Expand Down Expand Up @@ -45,6 +48,9 @@ public function offsetGet(mixed $offset): mixed
return $this->defaults[$offset];
}

/**
* @param int $offset
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->defaults[$offset] = $value;
Expand Down
87 changes: 86 additions & 1 deletion src/Definition/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Cycle\Schema\Definition;

use Cycle\ORM\MapperInterface;
use Cycle\ORM\RepositoryInterface;
use Cycle\ORM\Select\ScopeInterface;
use Cycle\ORM\Select\SourceInterface;
use Cycle\Schema\Definition\Map\FieldMap;
use Cycle\Schema\Definition\Map\OptionMap;
use Cycle\Schema\Definition\Map\RelationMap;
Expand All @@ -12,27 +16,56 @@

/**
* Contains information about specific entity definition.
*
* @template TEntity of object
*/
final class Entity
{
private OptionMap $options;

/**
* @var non-empty-string|null
*/
private ?string $role = null;

/**
* @var class-string<TEntity>|null
*/
private ?string $class = null;

/**
* @var non-empty-string|null
*/
private ?string $database = null;

/**
* @var non-empty-string|null
*/
private ?string $tableName = null;

/**
* @var class-string<MapperInterface>|null
*/
private ?string $mapper = null;

/**
* @var class-string<SourceInterface>|null
*/
private ?string $source = null;

/**
* @var class-string<ScopeInterface>|null
*/
private ?string $scope = null;

/**
* @var class-string<RepositoryInterface<TEntity>>|null
*/
private ?string $repository = null;

/**
* @var class-string|class-string[]|non-empty-string|non-empty-string[]|null
*/
private array|string|null $typecast = null;

private array $schema = [];
Expand Down Expand Up @@ -70,73 +103,109 @@ public function getOptions(): OptionMap
return $this->options;
}

/**
* @param non-empty-string $role
*/
public function setRole(string $role): self
{
$this->role = $role;

return $this;
}

/**
* @return non-empty-string|null
*/
public function getRole(): ?string
{
return $this->role;
}

/**
* @param class-string<TEntity> $class
*/
public function setClass(string $class): self
{
$this->class = $class;

return $this;
}

/**
* @return class-string<TEntity>|null
*/
public function getClass(): ?string
{
return $this->class;
}

/**
* @param class-string<MapperInterface>|null $mapper
*/
public function setMapper(?string $mapper): self
{
$this->mapper = $mapper;

return $this;
}

/**
* @return class-string<MapperInterface>|null
*/
public function getMapper(): ?string
{
return $this->normalizeClass($this->mapper);
}

/**
* @param class-string<SourceInterface>|null $source
*/
public function setSource(?string $source): self
{
$this->source = $source;

return $this;
}

/**
* @return class-string<SourceInterface>|null
*/
public function getSource(): ?string
{
return $this->normalizeClass($this->source);
}

/**
* @param class-string<ScopeInterface>|null $scope
*/
public function setScope(?string $scope): self
{
$this->scope = $scope;

return $this;
}

/**
* @return class-string<ScopeInterface>|null
*/
public function getScope(): ?string
{
return $this->normalizeClass($this->scope);
}

/**
* @param class-string<RepositoryInterface<TEntity>>|null $repository
*/
public function setRepository(?string $repository): self
{
$this->repository = $repository;

return $this;
}

/**
* @return class-string<RepositoryInterface<TEntity>>|null
*/
public function getRepository(): ?string
{
return $this->normalizeClass($this->repository);
Expand Down Expand Up @@ -278,13 +347,23 @@ public function getPrimaryFields(): FieldMap
return $this->primaryFields;
}

/**
* @template T of object
*
* @param class-string<T>|null $class
*
* @return ($class is class-string<T> ? class-string<T> : null)
*/
private function normalizeClass(string $class = null): ?string
{
if ($class === null) {
return null;
}

return ltrim($class, '\\');
/** @var class-string<T> $class */
$class = \ltrim($class, '\\');

return $class;
}

public function setInheritance(Inheritance $inheritance): void
Expand Down Expand Up @@ -318,6 +397,9 @@ public function getDatabase(): ?string
return $this->database;
}

/**
* @param non-empty-string|null $database
*/
public function setDatabase(?string $database): void
{
$this->database = $database;
Expand All @@ -328,6 +410,9 @@ public function getTableName(): ?string
return $this->tableName;
}

/**
* @param non-empty-string $tableName
*/
public function setTableName(string $tableName): void
{
$this->tableName = $tableName;
Expand Down
Loading

0 comments on commit db763c3

Please sign in to comment.