norse-blue/php-extensible-objects

View on GitHub
src/Guards/InvalidExtensionGuard.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace NorseBlue\ExtensibleObjects\Guards;

use NorseBlue\ExtensibleObjects\Contracts\ExtensionMethod;
use NorseBlue\ExtensibleObjects\Exceptions\ClassNotExtensionMethodException;

/**
 * @internal
 */
final class InvalidExtensionGuard
{
    /** @codeCoverageIgnore */
    private function __construct()
    {
    }

    /**
     * Guard against an invalid extension method class.
     *
     * @param string $extension The extension method class name.
     */
    public static function enforce(string $extension): void
    {
        if (! is_subclass_of($extension, ExtensionMethod::class)) {
            throw new ClassNotExtensionMethodException(
                sprintf(
                    "The extension method class '${extension}' must implement interface %s.",
                    ExtensionMethod::class
                )
            );
        }
    }
}