glastree_on_gitea

This commit is contained in:
2026-05-26 08:14:29 +02:00
commit 0bed099d05
9556 changed files with 1186307 additions and 0 deletions
+173
View File
@@ -0,0 +1,173 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* Somehow the standard reflection library doesn't include constants.
*
* ReflectionConstant corrects that omission.
*/
class ReflectionConstant implements \Reflector
{
public $name;
/** @var mixed */
private $value;
private const MAGIC_CONSTANTS = [
'__LINE__',
'__FILE__',
'__DIR__',
'__FUNCTION__',
'__CLASS__',
'__TRAIT__',
'__METHOD__',
'__NAMESPACE__',
'__COMPILER_HALT_OFFSET__',
];
/**
* Construct a ReflectionConstant object.
*
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
if (!\defined($name) && !self::isMagicConstant($name)) {
throw new \InvalidArgumentException('Unknown constant: '.$name);
}
if (!self::isMagicConstant($name)) {
$this->value = @\constant($name);
}
}
/**
* Exports a reflection.
*
* @param string $name
* @param bool $return pass true to return the export, as opposed to emitting it
*
* @return string|null
*/
public static function export(string $name, bool $return = false)
{
$refl = new self($name);
$value = $refl->getValue();
$str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
if ($return) {
return $str;
}
echo $str."\n";
return null;
}
public static function isMagicConstant($name)
{
return \in_array($name, self::MAGIC_CONSTANTS);
}
/**
* Get the constant's docblock.
*
* @return false
*/
public function getDocComment(): bool
{
return false;
}
/**
* Gets the constant name.
*/
public function getName(): string
{
return $this->name;
}
/**
* Gets the namespace name.
*
* Returns '' when the constant is not namespaced.
*/
public function getNamespaceName(): string
{
if (!$this->inNamespace()) {
return '';
}
return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
}
/**
* Gets the value of the constant.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Checks if this constant is defined in a namespace.
*/
public function inNamespace(): bool
{
return \strpos($this->name, '\\') !== false;
}
/**
* To string.
*/
public function __toString(): string
{
return $this->getName();
}
/**
* Gets the constant's file name.
*
* Currently returns null, because if it returns a file name the signature
* formatter will barf.
*/
public function getFileName()
{
return;
// return $this->class->getFileName();
}
/**
* Get the code start line.
*
* @throws \RuntimeException
*/
public function getStartLine()
{
throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
}
/**
* Get the code end line.
*
* @throws \RuntimeException
*/
public function getEndLine()
{
return $this->getStartLine();
}
}
@@ -0,0 +1,159 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* A fake ReflectionFunction but for language constructs.
*/
class ReflectionLanguageConstruct extends \ReflectionFunctionAbstract
{
public $keyword;
/**
* Language construct parameter definitions.
*/
private const LANGUAGE_CONSTRUCTS = [
'isset' => [
'var' => [],
'...' => [
'isOptional' => true,
'defaultValue' => null,
],
],
'unset' => [
'var' => [],
'...' => [
'isOptional' => true,
'defaultValue' => null,
],
],
'empty' => [
'var' => [],
],
'echo' => [
'arg1' => [],
'...' => [
'isOptional' => true,
'defaultValue' => null,
],
],
'print' => [
'arg' => [],
],
'die' => [
'status' => [
'isOptional' => true,
'defaultValue' => 0,
],
],
'exit' => [
'status' => [
'isOptional' => true,
'defaultValue' => 0,
],
],
];
/**
* Construct a ReflectionLanguageConstruct object.
*
* @param string $keyword
*/
public function __construct(string $keyword)
{
if (!self::isLanguageConstruct($keyword)) {
throw new \InvalidArgumentException('Unknown language construct: '.$keyword);
}
$this->keyword = $keyword;
}
/**
* This can't (and shouldn't) do anything :).
*
* @throws \RuntimeException
*/
public static function export($name)
{
throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
}
/**
* Get language construct name.
*/
public function getName(): string
{
return $this->keyword;
}
/**
* None of these return references.
*/
public function returnsReference(): bool
{
return false;
}
/**
* Get language construct params.
*
* @return array
*/
public function getParameters(): array
{
$params = [];
foreach (self::LANGUAGE_CONSTRUCTS[$this->keyword] as $parameter => $opts) {
$params[] = new ReflectionLanguageConstructParameter($this->keyword, $parameter, $opts);
}
return $params;
}
/**
* Gets the file name from a language construct.
*
* (Hint: it always returns false)
*
* @todo remove \ReturnTypeWillChange attribute after dropping support for PHP 7.x (when we can use union types)
*
* @return string|false (false)
*/
#[\ReturnTypeWillChange]
public function getFileName()
{
return false;
}
/**
* To string.
*/
public function __toString(): string
{
return $this->getName();
}
/**
* Check whether keyword is a (known) language construct.
*
* @param string $keyword
*/
public static function isLanguageConstruct(string $keyword): bool
{
return \array_key_exists($keyword, self::LANGUAGE_CONSTRUCTS);
}
}
@@ -0,0 +1,115 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* A fake ReflectionParameter but for language construct parameters.
*
* It stubs out all the important bits and returns whatever was passed in $opts.
*/
class ReflectionLanguageConstructParameter extends \ReflectionParameter
{
/** @var string|array|object */
private $function;
/** @var int|string */
private $parameter;
private array $opts;
/**
* @param string|array|object $function
* @param int|string $parameter
* @param array $opts
*/
public function __construct($function, $parameter, array $opts)
{
$this->function = $function;
$this->parameter = $parameter;
$this->opts = $opts;
}
/**
* No class here.
*/
public function getClass(): ?\ReflectionClass
{
return null;
}
/**
* Is the param an array?
*
* @return bool
*/
public function isArray(): bool
{
return \array_key_exists('isArray', $this->opts) && $this->opts['isArray'];
}
/**
* Get param default value.
*
* @todo remove \ReturnTypeWillChange attribute after dropping support for PHP 7.x (when we can use mixed type)
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getDefaultValue()
{
if ($this->isDefaultValueAvailable()) {
return $this->opts['defaultValue'];
}
return null;
}
/**
* Get param name.
*
* @return string
*/
public function getName(): string
{
return $this->parameter;
}
/**
* Is the param optional?
*
* @return bool
*/
public function isOptional(): bool
{
return \array_key_exists('isOptional', $this->opts) && $this->opts['isOptional'];
}
/**
* Does the param have a default value?
*
* @return bool
*/
public function isDefaultValueAvailable(): bool
{
return \array_key_exists('defaultValue', $this->opts);
}
/**
* Is the param passed by reference?
*
* (I don't think this is true for anything we need to fake a param for)
*
* @return bool
*/
public function isPassedByReference(): bool
{
return \array_key_exists('isPassedByReference', $this->opts) && $this->opts['isPassedByReference'];
}
}
@@ -0,0 +1,246 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* A fake ReflectionMethod for magic methods declared via @method docblock tags.
*
* This allows magic methods to be treated uniformly with real methods throughout
* PsySH, including in SignatureFormatter, the ls command, and tab completion.
*
* Note: This implements \Reflector but does not extend \ReflectionMethod because
* PHP's internal reflection classes have read-only properties that cannot be set.
*/
class ReflectionMagicMethod implements \Reflector
{
private \ReflectionClass $declaringClass;
public string $name;
public string $class;
private bool $isStatic;
private ?string $returnType;
private string $parameters;
private ?string $description;
private bool $returnsReference;
/**
* Construct a ReflectionMagicMethod.
*
* @param \ReflectionClass $declaringClass The class that declares this magic method
* @param string $name The method name
* @param bool $isStatic Whether this is a static method
* @param string|null $returnType The return type (from docblock)
* @param string $parameters The parameter string (without parentheses)
* @param string|null $description The method description
* @param bool $returnsReference Whether the method returns by reference
*/
public function __construct(
\ReflectionClass $declaringClass,
string $name,
bool $isStatic = false,
?string $returnType = null,
string $parameters = '',
?string $description = null,
bool $returnsReference = false
) {
$this->declaringClass = $declaringClass;
$this->name = $name;
$this->class = $declaringClass->getName();
$this->isStatic = $isStatic;
$this->returnType = $returnType;
$this->parameters = $parameters;
$this->description = $description;
$this->returnsReference = $returnsReference;
}
/**
* Get the method name.
*/
public function getName(): string
{
return $this->name;
}
/**
* Get the class that declares this method.
*/
public function getDeclaringClass(): \ReflectionClass
{
return $this->declaringClass;
}
/**
* Check if this is a static method.
*/
public function isStatic(): bool
{
return $this->isStatic;
}
/**
* Magic methods are always public.
*/
public function isPublic(): bool
{
return true;
}
/**
* Magic methods are never protected.
*/
public function isProtected(): bool
{
return false;
}
/**
* Magic methods are never private.
*/
public function isPrivate(): bool
{
return false;
}
/**
* Magic methods are never abstract.
*/
public function isAbstract(): bool
{
return false;
}
/**
* Magic methods are never final.
*/
public function isFinal(): bool
{
return false;
}
/**
* Check if this method returns by reference.
*/
public function returnsReference(): bool
{
return $this->returnsReference;
}
/**
* Get the method modifiers.
*/
public function getModifiers(): int
{
$modifiers = \ReflectionMethod::IS_PUBLIC;
if ($this->isStatic) {
$modifiers |= \ReflectionMethod::IS_STATIC;
}
return $modifiers;
}
/**
* Get parameters - returns empty array since we only have a string representation.
*
* @return \ReflectionParameter[]
*/
public function getParameters(): array
{
return [];
}
/**
* Get the raw parameter string from the docblock.
*/
public function getParameterString(): string
{
return $this->parameters;
}
/**
* Get the return type from the docblock.
*/
public function getDocblockReturnType(): ?string
{
return $this->returnType;
}
/**
* Get the description from the docblock.
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Magic methods don't have a native return type.
*/
public function hasReturnType(): bool
{
return false;
}
/**
* Magic methods don't have a native return type.
*/
public function getReturnType(): ?\ReflectionType
{
return null;
}
/**
* Get the docblock for this magic method.
*
* Returns the description if available.
*
* @return string|false
*/
public function getDocComment()
{
if ($this->description === null) {
return false;
}
return \sprintf("/**\n * %s\n */", $this->description);
}
/**
* Export is not supported.
*
* @throws \RuntimeException
*/
public static function export($class, $name, $return = false): ?string
{
throw new \RuntimeException('Export is not supported for magic methods');
}
/**
* String representation.
*/
public function __toString(): string
{
$static = $this->isStatic ? 'static ' : '';
$return = $this->returnType ? $this->returnType.' ' : '';
$ref = $this->returnsReference ? '&' : '';
return \sprintf(
'Method [ <magic> public %s%smethod %s%s ] { %s%s(%s) }',
$static,
$return,
$ref,
$this->name,
$ref,
$this->name,
$this->parameters
);
}
}
@@ -0,0 +1,219 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* A fake ReflectionProperty for magic properties declared via @property docblock tags.
*
* This allows magic properties to be treated uniformly with real properties throughout
* PsySH, including in SignatureFormatter, the ls command, and tab completion.
*
* Note: This implements \Reflector but does not extend \ReflectionProperty because
* PHP's internal reflection classes have read-only properties that cannot be set.
*/
class ReflectionMagicProperty implements \Reflector
{
private \ReflectionClass $declaringClass;
public string $name;
public string $class;
private ?string $type;
private bool $readOnly;
private bool $writeOnly;
private ?string $description;
/**
* Construct a ReflectionMagicProperty.
*
* @param \ReflectionClass $declaringClass The class that declares this magic property
* @param string $name The property name (without $)
* @param string|null $type The property type (from docblock)
* @param bool $readOnly Whether this is a read-only property (@property)
* @param bool $writeOnly Whether this is a write-only property (@property)
* @param string|null $description The property description
*/
public function __construct(
\ReflectionClass $declaringClass,
string $name,
?string $type = null,
bool $readOnly = false,
bool $writeOnly = false,
?string $description = null
) {
$this->declaringClass = $declaringClass;
$this->name = $name;
$this->class = $declaringClass->getName();
$this->type = $type;
$this->readOnly = $readOnly;
$this->writeOnly = $writeOnly;
$this->description = $description;
}
/**
* Get the property name.
*/
public function getName(): string
{
return $this->name;
}
/**
* Get the class that declares this property.
*/
public function getDeclaringClass(): \ReflectionClass
{
return $this->declaringClass;
}
/**
* Magic properties are always public.
*/
public function isPublic(): bool
{
return true;
}
/**
* Magic properties are never protected.
*/
public function isProtected(): bool
{
return false;
}
/**
* Magic properties are never private.
*/
public function isPrivate(): bool
{
return false;
}
/**
* Magic properties are never static.
*/
public function isStatic(): bool
{
return false;
}
/**
* Check if this is a read-only property (@property).
*/
public function isReadOnly(): bool
{
return $this->readOnly;
}
/**
* Check if this is a write-only property (@property).
*/
public function isWriteOnly(): bool
{
return $this->writeOnly;
}
/**
* Magic properties don't have default values.
*/
public function isDefault(): bool
{
return false;
}
/**
* Get the property modifiers.
*/
public function getModifiers(): int
{
return \ReflectionProperty::IS_PUBLIC;
}
/**
* Get the docblock type for this property.
*/
public function getDocblockType(): ?string
{
return $this->type;
}
/**
* Get the description from the docblock.
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Magic properties don't have a native type.
*/
public function hasType(): bool
{
return false;
}
/**
* Magic properties don't have a native type.
*/
public function getType(): ?\ReflectionType
{
return null;
}
/**
* Get the docblock for this magic property.
*
* Returns the description if available.
*
* @return string|false
*/
public function getDocComment()
{
if ($this->description === null) {
return false;
}
return \sprintf("/**\n * %s\n */", $this->description);
}
/**
* Export is not supported.
*
* @throws \RuntimeException
*/
public static function export($class, $name, $return = false): ?string
{
throw new \RuntimeException('Export is not supported for magic properties');
}
/**
* String representation.
*/
public function __toString(): string
{
$type = $this->type ? $this->type.' ' : '';
$suffix = '';
if ($this->readOnly) {
$suffix = ' (read-only)';
} elseif ($this->writeOnly) {
$suffix = ' (write-only)';
}
return \sprintf(
'Property [ <magic> public %s$%s ]%s',
$type,
$this->name,
$suffix
);
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2026 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* A fake Reflector for namespaces.
*/
class ReflectionNamespace implements \Reflector
{
private string $name;
/**
* Construct a ReflectionNamespace object.
*
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
}
/**
* Gets the constant name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* This can't (and shouldn't) do anything :).
*
* @throws \RuntimeException
*/
public static function export($name)
{
throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
}
/**
* To string.
*
* @return string
*/
public function __toString(): string
{
return $this->getName();
}
}