Skip to content

Commit 3a87188

Browse files
committed
add property hook
1 parent 403c9e9 commit 3a87188

File tree

4 files changed

+91
-26
lines changed

4 files changed

+91
-26
lines changed

README.md

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,13 +1761,13 @@ declare(strict_types=1);
17611761

17621762
use PhpParser\Node\Scalar\Float_;
17631763

1764-
return new Float_(100);
1764+
return new Float_(100.5);
17651765
```
17661766

17671767
17681768

17691769
```php
1770-
100.0
1770+
100.5
17711771
```
17721772

17731773
<br>
@@ -1922,6 +1922,42 @@ $variableName
19221922

19231923
<br>
19241924

1925+
## `PhpParser\Node\Stmt\Block`
1926+
1927+
### Example PHP Code
1928+
1929+
```php
1930+
<?php
1931+
1932+
declare(strict_types=1);
1933+
1934+
use PhpParser\Node\Expr\Assign;
1935+
use PhpParser\Node\Expr\Variable;
1936+
use PhpParser\Node\Scalar\LNumber;
1937+
use PhpParser\Node\Stmt\Block;
1938+
use PhpParser\Node\Stmt\Expression;
1939+
1940+
$assign = new Assign(new Variable('someValue'), new LNumber(10000));
1941+
1942+
return new Block([new Expression($assign)]);
1943+
```
1944+
1945+
1946+
1947+
```php
1948+
{
1949+
$someValue = 10000;
1950+
}
1951+
```
1952+
1953+
<br>
1954+
1955+
### Public Properties
1956+
1957+
* `$stmts` - `/** @var Stmt[] Statements */`
1958+
1959+
<br>
1960+
19251961
## `PhpParser\Node\Stmt\ClassConst`
19261962

19271963
### Example PHP Code
@@ -2580,6 +2616,38 @@ public string $propertyName;
25802616

25812617
declare(strict_types=1);
25822618

2619+
use PhpParser\Modifiers;
2620+
2621+
$propertyItem = new \PhpParser\Node\PropertyItem('someProperty');
2622+
$property = new \PhpParser\Node\Stmt\Property(Modifiers::PUBLIC, [$propertyItem]);
2623+
2624+
$plus = new \PhpParser\Node\Expr\BinaryOp\Plus(
2625+
new \PhpParser\Node\Expr\Variable('variable'),
2626+
new \PhpParser\Node\Scalar\Int_(100)
2627+
);
2628+
2629+
$getPropertyHook = new \PhpParser\Node\PropertyHook('getProperty', $plus);
2630+
2631+
$property->hooks[] = $getPropertyHook;
2632+
2633+
return $property;
2634+
```
2635+
2636+
2637+
2638+
```php
2639+
public $someProperty {
2640+
getProperty => $variable + 100;
2641+
}
2642+
```
2643+
2644+
<br>
2645+
2646+
```php
2647+
<?php
2648+
2649+
declare(strict_types=1);
2650+
25832651
use PhpParser\Node\Stmt\Property;
25842652
use PhpParser\Node\Stmt\PropertyProperty;
25852653
use PhpParser\Node\VarLikeIdentifier;
@@ -2754,28 +2822,6 @@ return new Alias($traitFullyQualified, 'method', Modifiers::PUBLIC, 'aliasedMeth
27542822

27552823
<br>
27562824

2757-
```php
2758-
<?php
2759-
2760-
declare(strict_types=1);
2761-
2762-
use PhpParser\Modifiers;
2763-
use PhpParser\Node\Name\FullyQualified;
2764-
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
2765-
2766-
$traitFullyQualified = new FullyQualified('TraitName');
2767-
2768-
return new Alias($traitFullyQualified, 'method', Modifiers::PUBLIC, 'aliasedMethod');
2769-
```
2770-
2771-
2772-
2773-
```php
2774-
\TraitName::method as public aliasedMethod;
2775-
```
2776-
2777-
<br>
2778-
27792825
### Public Properties
27802826

27812827
* `$newModifier` - `/** @var null|int New modifier */`

snippet/float_.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
use PhpParser\Node\Scalar\Float_;
66

7-
return new Float_(100);
7+
return new Float_(100.5);

snippet/php_84/property_hook.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpParser\Modifiers;
6+
7+
$propertyItem = new \PhpParser\Node\PropertyItem('someProperty');
8+
$property = new \PhpParser\Node\Stmt\Property(Modifiers::PUBLIC, [$propertyItem]);
9+
10+
$plus = new \PhpParser\Node\Expr\BinaryOp\Plus(
11+
new \PhpParser\Node\Expr\Variable('variable'),
12+
new \PhpParser\Node\Scalar\Int_(100)
13+
);
14+
15+
$getPropertyHook = new \PhpParser\Node\PropertyHook('getProperty', $plus);
16+
17+
$property->hooks[] = $getPropertyHook;
18+
19+
return $property;

src/NodeCodeSampleProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
final class NodeCodeSampleProvider
1414
{
15-
private Standard $standardPrinter;
15+
private readonly Standard $standardPrinter;
1616

1717
public function __construct(
1818
) {

0 commit comments

Comments
 (0)