1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-08 14:08:43 +01:00
psalm/src/Psalm/Internal/MethodIdentifier.php

75 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal;
2021-12-03 21:40:18 +01:00
use InvalidArgumentException;
use Psalm\Storage\ImmutableNonCloneableTrait;
2021-12-03 21:40:18 +01:00
2021-12-03 21:07:25 +01:00
use function explode;
use function is_string;
2022-09-13 18:33:47 +02:00
use function ltrim;
2021-12-03 21:07:25 +01:00
use function strpos;
use function strtolower;
/**
* @psalm-immutable
2022-01-03 07:55:32 +01:00
* @internal
*/
class MethodIdentifier
{
use ImmutableNonCloneableTrait;
2022-12-14 01:52:54 +01:00
public string $fq_class_name;
2022-12-14 04:34:34 +01:00
/** @var lowercase-string */
public string $method_name;
/**
2022-12-14 04:34:34 +01:00
* @param lowercase-string $method_name
*/
public function __construct(string $fq_class_name, string $method_name)
{
$this->fq_class_name = $fq_class_name;
$this->method_name = $method_name;
}
/**
* Takes any valid reference to a method id and converts
* it into a MethodIdentifier
2020-08-24 00:07:49 +02:00
*
* @param string|MethodIdentifier $method_id
2020-08-24 00:07:49 +02:00
* @psalm-pure
*/
public static function wrap($method_id): self
{
2021-12-03 21:07:25 +01:00
return is_string($method_id) ? static::fromMethodIdReference($method_id) : $method_id;
}
2020-08-23 19:52:31 +02:00
/**
* @psalm-pure
*/
public static function isValidMethodIdReference(string $method_id): bool
{
2021-12-03 21:07:25 +01:00
return strpos($method_id, '::') !== false;
}
2020-08-23 20:02:10 +02:00
/**
* @psalm-pure
*/
public static function fromMethodIdReference(string $method_id): self
{
if (!static::isValidMethodIdReference($method_id)) {
2021-12-03 21:40:18 +01:00
throw new InvalidArgumentException('Invalid method id reference provided: ' . $method_id);
}
// remove leading backslash if it exists
$method_id = ltrim($method_id, '\\');
2021-12-03 21:07:25 +01:00
$method_id_parts = explode('::', $method_id);
return new self($method_id_parts[0], strtolower($method_id_parts[1]));
}
2020-05-15 16:18:05 +02:00
/** @return non-empty-string */
public function __toString(): string
{
return $this->fq_class_name . '::' . $this->method_name;
}
}