Skip to content

Commit

Permalink
Make DefinitionValidator::validateArrayDefinition() public and remo…
Browse files Browse the repository at this point in the history
…ve `ParameterDefinition::isBuiltin()` (#37)
  • Loading branch information
vjik committed Jun 13, 2022
1 parent 98bb455 commit d1953e4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,9 @@
# Yii Definitions Change Log

## 1.0.3 under development
## 2.0.0 under development

- no changes in this release.
- New #37: Make method `DefinitionValidator::validateArrayDefinition()` public (vjik)
- Chg #37: Remove method `ParameterDefinition::isBuiltin()` (vjik)

## 1.0.2 April 01, 2022

Expand Down
6 changes: 5 additions & 1 deletion src/DefinitionStorage.php
Expand Up @@ -147,7 +147,11 @@ private function isResolvable(string $id, array $building): bool
if ($type instanceof ReflectionUnionType) {
$isUnionTypeResolvable = false;
$unionTypes = [];
/** @var ReflectionNamedType $unionType */
/**
* @psalm-suppress UnnecessaryVarAnnotation Annotation below is needed in PHP 7.4
*
* @var ReflectionNamedType $unionType
*/
foreach ($type->getTypes() as $unionType) {
if (!$unionType->isBuiltin()) {
$typeName = $unionType->getName();
Expand Down
8 changes: 6 additions & 2 deletions src/Helpers/DefinitionValidator.php
Expand Up @@ -55,9 +55,13 @@ public static function validate($definition, ?string $id = null): void
}

/**
* @throws InvalidConfigException
* Validates that array definition is valid. Throws exception otherwise.
*
* @param array $definition Array definition to validate.
*
* @throws InvalidConfigException If definition is not valid.
*/
private static function validateArrayDefinition(array $definition, ?string $id): void
public static function validateArrayDefinition(array $definition, ?string $id = null): void
{
foreach ($definition as $key => $value) {
if (!is_string($key)) {
Expand Down
29 changes: 15 additions & 14 deletions src/ParameterDefinition.php
Expand Up @@ -45,15 +45,6 @@ public function isOptional(): bool
return $this->parameter->isOptional();
}

public function isBuiltin(): bool
{
$type = $this->parameter->getType();
if ($type === null) {
return false;
}
return $type->isBuiltin();
}

public function hasValue(): bool
{
return $this->parameter->isDefaultValueAvailable();
Expand All @@ -71,7 +62,11 @@ public function resolve(ContainerInterface $container)
return $this->resolveUnionType($container);
}

if (!$this->isBuiltin()) {
/** @var ReflectionNamedType|null $type */
$type = $this->parameter->getType();
$isBuiltin = $type !== null && $type->isBuiltin();

if (!$isBuiltin) {
/** @var ReflectionNamedType $type */
$typeName = $type->getName();
if ($typeName === 'self') {
Expand Down Expand Up @@ -167,22 +162,28 @@ private function resolveVariadicOrBuiltinOrNonTyped()
private function resolveUnionType(ContainerInterface $container)
{
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress UndefinedDocblockClass This annotation is needed in PHP 7.4
*
* @var ReflectionUnionType $parameterType
*/
$parameterType = $this->parameter->getType();

/**
* @var \ReflectionType[] $types
* @psalm-suppress UndefinedClass
* @psalm-suppress UndefinedDocblockClass This annotation is needed in PHP 7.4
*
* @var ReflectionNamedType[] $types
*/
$types = $parameterType->getTypes();
$class = implode('|', $types);

foreach ($types as $type) {
if (!$type->isBuiltin()) {
/** @var ReflectionNamedType $type */
$typeName = $type->getName();
/**
* @psalm-suppress TypeDoesNotContainType
*
* @link https://github.com/vimeo/psalm/issues/6756
*/
if ($typeName === 'self') {
// If type name is "self", it means that called class and
// $parameter->getDeclaringClass() returned instance of `ReflectionClass`.
Expand Down
28 changes: 0 additions & 28 deletions tests/Unit/ParameterDefinitionTest.php
Expand Up @@ -234,34 +234,6 @@ public function testNotInstantiablePhpInternal(): void
$definition->resolve($container);
}

public function dataIsBuiltin(): array
{
return [
[
true,
$this->getFirstParameter(static fn (int $n) => 42),
],
[
false,
$this->getFirstParameter(static fn (Car $car) => 42),
],
[
false,
$this->getFirstParameter(static fn ($x) => 42),
],
];
}

/**
* @dataProvider dataIsBuiltin
*/
public function testIsBuiltin(bool $expected, ReflectionParameter $parameter): void
{
$definition = new ParameterDefinition($parameter);

$this->assertSame($expected, $definition->isBuiltin());
}

public function testOptionalBrokenDependency(): void
{
$container = new SimpleContainer(
Expand Down

0 comments on commit d1953e4

Please sign in to comment.