Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the ability to monkey patch the code #908

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.

24 changes: 12 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ Here's an example of `MakeLocatorForComposerJsonAndInstalledJson` usage:
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForComposerJsonAndInstalledJson;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new AggregateSourceLocator([
$reflector = new DefaultReflector(new AggregateSourceLocator([
(new MakeLocatorForComposerJsonAndInstalledJson)('path/to/the/project', $astLocator),
new PhpInternalSourceLocator($astLocator, new ReflectionSourceStubber())
]));
Expand All @@ -157,13 +158,13 @@ $classes = $reflector->reflectAllClasses();
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator;

$classLoader = require 'vendor/autoload.php';

$astLocator = (new BetterReflection())->astLocator();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new ComposerSourceLocator($classLoader, $astLocator));
$reflector = new DefaultReflector(new ComposerSourceLocator($classLoader, $astLocator));
$reflectionClass = $reflector->reflectClass('Foo\Bar\MyClass');

echo $reflectionClass->getShortName(); // MyClass
Expand All @@ -177,11 +178,11 @@ echo $reflectionClass->getNamespaceName(); // Foo\Bar
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new SingleFileSourceLocator('path/to/MyApp/MyClass.php', $astLocator));
$reflector = new DefaultReflector(new SingleFileSourceLocator('path/to/MyApp/MyClass.php', $astLocator));
$reflectionClass = $reflector->reflectClass('MyApp\MyClass');

echo $reflectionClass->getShortName(); // MyClass
Expand All @@ -195,13 +196,13 @@ echo $reflectionClass->getNamespaceName(); // MyApp
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;

$code = '<?php class Foo {};';

$astLocator = (new BetterReflection())->astLocator();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new StringSourceLocator($code, $astLocator));
$reflector = new DefaultReflector(new StringSourceLocator($code, $astLocator));
$reflectionClass = $reflector->reflectClass('Foo');

echo $reflectionClass->getShortName(); // Foo
Expand All @@ -213,11 +214,11 @@ echo $reflectionClass->getShortName(); // Foo
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector(new SingleFileSourceLocator('path/to/file.php', $astLocator));
$reflector = new DefaultReflector(new SingleFileSourceLocator('path/to/file.php', $astLocator));
$classes = $reflector->reflectAllClasses();
```

Expand All @@ -227,12 +228,12 @@ $classes = $reflector->reflectAllClasses();
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\Reflector\DefaultReflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;

$astLocator = (new BetterReflection())->astLocator();
$directoriesSourceLocator = new DirectoriesSourceLocator(['path/to/directory1'], $astLocator);
$reflector = new \Roave\BetterReflection\Reflector\DefaultReflector($directoriesSourceLocator);
$reflector = new DefaultReflector($directoriesSourceLocator);
$classes = $reflector->reflectAllClasses();
```

Expand All @@ -252,7 +253,6 @@ See example in "Reflecting Classes" section on the same subheading.
<?php

use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;

$configuration = new BetterReflection();
Expand Down
106 changes: 0 additions & 106 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
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;
}
}
Loading