Skip to content

Commit

Permalink
Types::isSingle() -> isSimple()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 17, 2023
1 parent 3dabec5 commit 912db4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
31 changes: 19 additions & 12 deletions src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Type
private $types;

/** @var bool */
private $single;
private $simple;

/** @var string |, & */
private $kind;
Expand Down Expand Up @@ -117,14 +117,14 @@ private function __construct(array $types, string $kind = '|')
}

$this->types = $types;
$this->single = ($types[1] ?? 'null') === 'null';
$this->simple = ($types[1] ?? 'null') === 'null';
$this->kind = count($types) > 1 ? $kind : '';
}


public function __toString(): string
{
return $this->single
return $this->simple
? (count($this->types) > 1 ? '?' : '') . $this->types[0]
: implode($this->kind, $this->types);
}
Expand All @@ -151,11 +151,11 @@ public function getTypes(): array


/**
* Returns the type name for single types, otherwise null.
* Returns the type name for simple types, otherwise null.
*/
public function getSingleName(): ?string
{
return $this->single
return $this->simple
? $this->types[0]
: null;
}
Expand All @@ -180,29 +180,36 @@ public function isIntersection(): bool


/**
* Returns true whether it is a single type. Simple nullable types are also considered to be single types.
* Returns true whether it is a simple type. Single nullable types are also considered to be simple types.
*/
public function isSimple(): bool
{
return $this->simple;
}


/** @deprecated use isSimple() */
public function isSingle(): bool
{
return $this->single;
return $this->simple;
}


/**
* Returns true whether the type is both a single and a PHP built-in type.
* Returns true whether the type is both a simple and a PHP built-in type.
*/
public function isBuiltin(): bool
{
return $this->single && Validators::isBuiltinType($this->types[0]);
return $this->simple && Validators::isBuiltinType($this->types[0]);
}


/**
* Returns true whether the type is both a single and a class name.
* Returns true whether the type is both a simple and a class name.
*/
public function isClass(): bool
{
return $this->single && !Validators::isBuiltinType($this->types[0]);
return $this->simple && !Validators::isBuiltinType($this->types[0]);
}


Expand All @@ -211,7 +218,7 @@ public function isClass(): bool
*/
public function isClassKeyword(): bool
{
return $this->single && Validators::isClassKeyword($this->types[0]);
return $this->simple && Validators::isClassKeyword($this->types[0]);
}


Expand Down
20 changes: 10 additions & 10 deletions tests/Utils/Type.fromString.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Assert::same('string', $type->getSingleName());
Assert::false($type->isClass());
Assert::false($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -35,7 +35,7 @@ Assert::same('string', $type->getSingleName());
Assert::false($type->isClass());
Assert::true($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -49,7 +49,7 @@ Assert::same('string', $type->getSingleName());
Assert::false($type->isClass());
Assert::true($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -63,7 +63,7 @@ Assert::same('string', $type->getSingleName());
Assert::false($type->isClass());
Assert::true($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -77,7 +77,7 @@ Assert::same('NS\Foo', $type->getSingleName());
Assert::true($type->isClass());
Assert::false($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::false($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -91,7 +91,7 @@ Assert::null($type->getSingleName());
Assert::false($type->isClass());
Assert::true($type->isUnion());
Assert::false($type->isIntersection());
Assert::false($type->isSingle());
Assert::false($type->isSimple());
Assert::false($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand Down Expand Up @@ -119,7 +119,7 @@ Assert::same('mixed', $type->getSingleName());
Assert::false($type->isClass());
Assert::false($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -133,7 +133,7 @@ Assert::same('null', $type->getSingleName());
Assert::false($type->isClass());
Assert::false($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::true($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -147,7 +147,7 @@ Assert::null($type->getSingleName());
Assert::false($type->isClass());
Assert::false($type->isUnion());
Assert::true($type->isIntersection());
Assert::false($type->isSingle());
Assert::false($type->isSimple());
Assert::false($type->isBuiltin());
Assert::false($type->isClassKeyword());

Expand All @@ -161,6 +161,6 @@ Assert::same('self', $type->getSingleName());
Assert::true($type->isClass());
Assert::false($type->isUnion());
Assert::false($type->isIntersection());
Assert::true($type->isSingle());
Assert::true($type->isSimple());
Assert::false($type->isBuiltin());
Assert::true($type->isClassKeyword());

0 comments on commit 912db4a

Please sign in to comment.