Skip to content

Commit

Permalink
Removed exceptionally.php
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 20, 2024
1 parent 5006a59 commit 7ffa117
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 48 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
"autoload": {
"psr-4": {
"Typhoon\\Reflection\\": "src/"
},
"files": [
"src/Exceptionally/exceptionally.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
30 changes: 0 additions & 30 deletions src/Exceptionally/exceptionally.php

This file was deleted.

26 changes: 19 additions & 7 deletions src/Metadata/ChangeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Typhoon\Reflection\Metadata;

use function Typhoon\Reflection\Exceptionally\exceptionally;
use Typhoon\Reflection\Exception\DefaultReflectionException;

/**
* @internal
Expand All @@ -18,7 +18,11 @@ abstract class ChangeDetector
final public static function fromFileContents(string $file, ?string $contents = null): self
{
if ($contents === null) {
$hash = exceptionally(static fn(): string|false => md5_file($file));
$hash = md5_file($file);

if ($hash === false) {
throw new DefaultReflectionException(sprintf('File %s does not exist or is not readable.', $file));
}
} else {
$hash = md5($contents);
}
Expand All @@ -34,12 +38,20 @@ final public static function fromReflection(\ReflectionClass $reflection): self
return self::fromFileContents($file);
}

$extension = $reflection->getExtensionName();
$extension = $reflection->getExtension();

if ($extension !== null) {
$name = $extension->name;
\assert($name !== '');

return new PhpVersionChangeDetector($name, $extension->getVersion() ?? false);
}

if ($reflection->isInternal()) {
return new PhpVersionChangeDetector(null, PHP_VERSION);
}

return new PhpVersionChangeDetector(
$extension,
exceptionally(static fn(): string|false => phpversion($extension)),
);
throw new DefaultReflectionException();
}

abstract public function changed(): bool;
Expand Down
10 changes: 5 additions & 5 deletions src/Metadata/FileChangeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Typhoon\Reflection\Metadata;

use function Typhoon\Reflection\Exceptionally\exceptionally;

/**
* @internal
* @psalm-internal Typhoon\Reflection
Expand All @@ -23,10 +21,12 @@ protected function __construct(

public function changed(): bool
{
set_error_handler(static fn(): bool => true);

try {
return exceptionally(fn(): string|false => md5_file($this->file)) !== $this->hash;
} catch (\Throwable) {
return true;
return md5_file($this->file) !== $this->hash;
} finally {
restore_error_handler();
}
}
}
10 changes: 8 additions & 2 deletions src/Metadata/PhpVersionChangeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ final class PhpVersionChangeDetector extends ChangeDetector
*/
protected function __construct(
private readonly ?string $extension,
private readonly string $version,
private readonly string|false $version,
) {}

public function changed(): bool
{
return phpversion($this->extension) === $this->version;
set_error_handler(static fn(): bool => true);

try {
return phpversion($this->extension) === $this->version;
} finally {
restore_error_handler();
}
}
}

0 comments on commit 7ffa117

Please sign in to comment.