Skip to content

Commit

Permalink
misc: improve internal definitions string types
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Feb 15, 2024
1 parent e692f0d commit 105281b
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/Definition/FunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
final class FunctionDefinition
{
public function __construct(
/** @var non-empty-string */
public readonly string $name,
/** @var non-empty-string */
public readonly string $signature,
public readonly Attributes $attributes,
/** @var non-empty-string|null */
public readonly ?string $fileName,
/** @var class-string|null */
public readonly ?string $class,
Expand Down
2 changes: 2 additions & 0 deletions src/Definition/MethodDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
final class MethodDefinition
{
public function __construct(
/** @var non-empty-string */
public readonly string $name,
/** @var non-empty-string */
public readonly string $signature,
public readonly Parameters $parameters,
public readonly bool $isStatic,
Expand Down
2 changes: 2 additions & 0 deletions src/Definition/ParameterDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
final class ParameterDefinition
{
public function __construct(
/** @var non-empty-string */
public readonly string $name,
/** @var non-empty-string */
public readonly string $signature,
public readonly Type $type,
public readonly bool $isOptional,
Expand Down
2 changes: 2 additions & 0 deletions src/Definition/PropertyDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
final class PropertyDefinition
{
public function __construct(
/** @var non-empty-string */
public readonly string $name,
/** @var non-empty-string */
public readonly string $signature,
public readonly Type $type,
public readonly bool $hasDefaultValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function __construct(AttributesRepository $attributesRepository)

public function for(ReflectionMethod $reflection, ReflectionTypeResolver $typeResolver): MethodDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;

$parameters = array_map(
fn (ReflectionParameter $parameter) => $this->parameterBuilder->for($parameter, $typeResolver),
$reflection->getParameters()
Expand All @@ -33,7 +36,7 @@ public function for(ReflectionMethod $reflection, ReflectionTypeResolver $typeRe
$returnType = $typeResolver->resolveType($reflection);

return new MethodDefinition(
$reflection->name,
$name,
Reflection::signature($reflection),
new Parameters(...$parameters),
$reflection->isStatic(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(private AttributesRepository $attributesRepository)

public function for(ReflectionParameter $reflection, ReflectionTypeResolver $typeResolver): ParameterDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$signature = Reflection::signature($reflection);
$type = $typeResolver->resolveType($reflection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(private AttributesRepository $attributesRepository)

public function for(ReflectionProperty $reflection, ReflectionTypeResolver $typeResolver): PropertyDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$signature = Reflection::signature($reflection);
$type = $typeResolver->resolveType($reflection);
Expand Down
3 changes: 3 additions & 0 deletions src/Utility/Reflection/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ static function (ReflectionAttribute $attribute) {
);
}

/**
* @return non-empty-string
*/
public static function signature(Reflector $reflection): string
{
if ($reflection instanceof ReflectionClass) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Fake/Definition/FakeFunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

final class FakeFunctionDefinition
{
/**
* @param non-empty-string|null $fileName
*/
public static function new(string $fileName = null): FunctionDefinition
{
return new FunctionDefinition(
Expand Down
7 changes: 6 additions & 1 deletion tests/Fake/Definition/FakeMethodDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ final class FakeMethodDefinition
{
private function __construct() {}

/**
* @param non-empty-string $name
*/
public static function new(string $name = 'someMethod'): MethodDefinition
{
return new MethodDefinition(
Expand All @@ -36,6 +39,8 @@ public static function constructor(): MethodDefinition

public static function fromReflection(ReflectionMethod $reflection): MethodDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$returnType = new MixedType();

if ($reflection->hasReturnType()) {
Expand All @@ -48,7 +53,7 @@ public static function fromReflection(ReflectionMethod $reflection): MethodDefin
);

return new MethodDefinition(
$reflection->name,
$name,
'Signature::' . $reflection->name,
new Parameters(...$parameters),
$reflection->isStatic(),
Expand Down
10 changes: 9 additions & 1 deletion tests/Fake/Definition/FakeParameterDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class FakeParameterDefinition
{
private function __construct() {}

/**
* @param non-empty-string $name
*/
public static function new(string $name = 'someParameter', Type $type = null): ParameterDefinition
{
return new ParameterDefinition(
Expand All @@ -27,6 +30,9 @@ public static function new(string $name = 'someParameter', Type $type = null): P
);
}

/**
* @param non-empty-string $name
*/
public static function optional(string $name, Type $type, mixed $defaultValue): ParameterDefinition
{
return new ParameterDefinition(
Expand All @@ -42,14 +48,16 @@ public static function optional(string $name, Type $type, mixed $defaultValue):

public static function fromReflection(ReflectionParameter $reflection): ParameterDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$type = new FakeType();

if ($reflection->hasType()) {
$type = FakeType::from($reflection->getType()->getName()); // @phpstan-ignore-line
}

return new ParameterDefinition(
$reflection->name,
$name,
'Signature::' . $reflection->name,
$type,
$reflection->isOptional(),
Expand Down
7 changes: 6 additions & 1 deletion tests/Fake/Definition/FakePropertyDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class FakePropertyDefinition
{
private function __construct() {}

/**
* @param non-empty-string $name
*/
public static function new(string $name = 'someProperty'): PropertyDefinition
{
return new PropertyDefinition(
Expand All @@ -29,6 +32,8 @@ public static function new(string $name = 'someProperty'): PropertyDefinition

public static function fromReflection(ReflectionProperty $reflection): PropertyDefinition
{
/** @var non-empty-string $name */
$name = $reflection->name;
$defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties();
$type = new MixedType();

Expand All @@ -37,7 +42,7 @@ public static function fromReflection(ReflectionProperty $reflection): PropertyD
}

return new PropertyDefinition(
$reflection->name,
$name,
'Signature::' . $reflection->name,
$type,
isset($defaultProperties[$reflection->name]),
Expand Down
10 changes: 7 additions & 3 deletions tests/Unit/Cache/FileWatchingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,22 @@ public function test_modifying_function_definition_file_invalids_cache(): void
{
$file = $this->functionDefinitionFile();

$function = FakeFunctionDefinition::new($file->url());
/** @var non-empty-string */
$url = $file->url();

$function = FakeFunctionDefinition::new($url);

self::assertTrue($this->cache->set('some-function-definition', $function));
self::assertTrue($this->cache->has('some-function-definition'));

unlink($file->url());
unlink($url);
$file->lastModified(time() + 5)->at($this->files);

self::assertFalse($this->cache->has('some-function-definition'));
self::assertTrue($this->cache->setMultiple(['some-function-definition' => $function]));
self::assertTrue($this->cache->has('some-function-definition'));

unlink($file->url());
unlink($url);
$file->lastModified(time() + 10)->at($this->files);

self::assertFalse($this->cache->has('some-function-definition'));
Expand All @@ -190,6 +193,7 @@ public function test_file_timestamps_are_fetched_once_per_request(): void
self::assertFalse($cacheB->has('some-function-definition'));
self::assertFalse($cacheB->has('some-function-definition'));

/** @var non-empty-string $file */
$file = $this->functionDefinitionFile()->url();

$cacheA->set('some-function-definition', FakeFunctionDefinition::new($file));
Expand Down

0 comments on commit 105281b

Please sign in to comment.