Skip to content

Commit

Permalink
Removed the ability to monkey patch the code
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Dec 6, 2021
1 parent 4844ea3 commit 437cec0
Show file tree
Hide file tree
Showing 18 changed files with 2 additions and 859 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -58,7 +58,6 @@ $classInfo = (new BetterReflection())
* [The features](docs/features.md)
* [Test suite](https://github.com/Roave/BetterReflection/blob/master/test/README.md)
* [AST extraction](docs/ast-extraction.md)
* [Reflection modification](docs/reflection-modification.md)

## Upgrading

Expand Down
1 change: 1 addition & 0 deletions UPGRADE.md
Expand Up @@ -6,6 +6,7 @@ or backwards compatibility (BC) breakages occur.
## 5.0.0

### BC Breaks
* Removed the ability to monkey patch the code.
* Doc block parsing has been removed:
* `\Roave\BetterReflection\Reflection\ReflectionMethod::getDocBlockReturnTypes()`
* `\Roave\BetterReflection\Reflection\ReflectionFunction::getDocBlockReturnTypes()`
Expand Down
8 changes: 0 additions & 8 deletions demo/monkey-patching/MyClass.php

This file was deleted.

25 changes: 0 additions & 25 deletions demo/monkey-patching/index.php

This file was deleted.

19 changes: 0 additions & 19 deletions docs/features.md
Expand Up @@ -29,22 +29,3 @@ These act in the same way as the core reflection API, except they return a
```php
$reflectionType = $parameterInfo->getType();
```

However, Better Reflection also gives the ability to change, and remove type declarations. Removing these types might
be useful if you want to make code written for PHP 7 work in PHP 5 for example, or setting new types to do the
opposite. For example, you might want to set the PHP 7 return type declaration to that defined in the PHP DocBlock.

```php
// Change a function to ensure it returns an integer
$functionInfo->setReturnType('int');

// Remove the return type declaration
$functionInfo->removeReturnType();
```

You can do similar things with parameter types also:

```php
$parameterInfo->setType('int');
$parameterInfo->removeType();
```
2 changes: 1 addition & 1 deletion docs/how-it-works.md
Expand Up @@ -66,4 +66,4 @@ Locators is loaded into an [AST format](https://en.wikipedia.org/wiki/Abstract_s

Internally to the `Reflection*` classes in Better Reflection, we cleverly hold the AST as the main property. All
reflection, analysis and modification is done directly to this AST. Therefore it is possible to unparse or export the
AST back into code - and thus execute monkey patched code.
AST back into code.
63 changes: 0 additions & 63 deletions docs/reflection-modification.md

This file was deleted.

106 changes: 0 additions & 106 deletions src/Reflection/ReflectionClass.php
Expand Up @@ -14,12 +14,10 @@
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property as PropertyNode;
use PhpParser\Node\Stmt\Trait_ as TraitNode;
use PhpParser\Node\Stmt\TraitUse;
use ReflectionClass as CoreReflectionClass;
use ReflectionException;
use ReflectionProperty as CoreReflectionProperty;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflection\Annotation\AnnotationHelper;
use Roave\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper;
Expand Down Expand Up @@ -1660,108 +1658,4 @@ public function getAttributesByInstance(string $className): array
{
return ReflectionAttributeHelper::filterAttributesByInstance($this->getAttributes(), $className);
}

/**
* Set whether this class is final or not
*
* @throws NotAClassReflection
*/
public function setFinal(bool $isFinal): void
{
if (! $this->node instanceof ClassNode) {
throw NotAClassReflection::fromReflectionClass($this);
}

if ($isFinal === true) {
$this->node->flags |= ClassNode::MODIFIER_FINAL;

return;
}

$this->node->flags &= ~ClassNode::MODIFIER_FINAL;
}

/**
* Remove the named method from the class.
*
* Returns true if method was successfully removed.
* Returns false if method was not found, or could not be removed.
*/
public function removeMethod(string $methodName): bool
{
$lowerName = strtolower($methodName);
foreach ($this->node->getMethods() as $key => $stmt) {
if ($lowerName === $stmt->name->toLowerString()) {
unset($this->node->stmts[$key]);
$this->cachedMethods = null;

return true;
}
}

return false;
}

/**
* Add a new method to the class.
*/
public function addMethod(string $methodName): void
{
$this->node->stmts[] = new ClassMethod($methodName);
$this->cachedMethods = null;
}

/**
* Add a new property to the class.
*
* Visibility defaults to \ReflectionProperty::IS_PUBLIC, or can be ::IS_PROTECTED or ::IS_PRIVATE.
*/
public function addProperty(
string $propertyName,
int $visibility = CoreReflectionProperty::IS_PUBLIC,
bool $static = false,
): void {
$type = 0;
switch ($visibility) {
case CoreReflectionProperty::IS_PRIVATE:
$type |= ClassNode::MODIFIER_PRIVATE;
break;
case CoreReflectionProperty::IS_PROTECTED:
$type |= ClassNode::MODIFIER_PROTECTED;
break;
default:
$type |= ClassNode::MODIFIER_PUBLIC;
break;
}

if ($static) {
$type |= ClassNode::MODIFIER_STATIC;
}

$this->node->stmts[] = new PropertyNode($type, [new Node\Stmt\PropertyProperty($propertyName)]);
$this->cachedProperties = null;
$this->cachedImmediateProperties = null;
}

/**
* Remove a property from the class.
*/
public function removeProperty(string $propertyName): bool
{
$lowerName = strtolower($propertyName);

foreach ($this->node->getProperties() as $key => $stmt) {
$propertyNames = array_map(static fn (Node\Stmt\PropertyProperty $propertyProperty): string => $propertyProperty->name->toLowerString(), $stmt->props);

if (in_array($lowerName, $propertyNames, true)) {
$this->cachedProperties = null;
$this->cachedImmediateProperties = null;
unset($this->node->stmts[$key]);

return true;
}
}

return false;
}
}

0 comments on commit 437cec0

Please sign in to comment.