Skip to content

Commit e37e715

Browse files
authored
Merge pull request #110 from ircmaxell/type_cleanups
Clean up types to support 8.1 more natively
2 parents 3b8d5bb + b86e2f0 commit e37e715

File tree

135 files changed

+4595
-1930
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+4595
-1930
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ vendor/
44
.idea/
55
.phpunit.result.cache
66
.php-cs-fixer.cache
7+
coverage

.php-cs-fixer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
->name('.php_cs')
1212
->exclude('vendor')
1313
->exclude('.git')
14+
->exclude('coverage')
1415
->in(__DIR__);
1516

1617
return (new PhpCsFixer\Config())
@@ -23,5 +24,11 @@
2324
'comment_type' => 'PHPDoc',
2425
'header' => $header,
2526
],
27+
'fully_qualified_strict_types' => true,
28+
'global_namespace_import' => true,
29+
'no_unused_imports' => true,
30+
'ordered_imports' => true,
31+
'single_line_after_imports' => true,
32+
'single_import_per_statement' => true,
2633
])
2734
->setFinder($finder);

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
SHELL := /bin/bash
2+
3+
targets=$(shell for file in `find . -name '*Test.php' -type f -printf "%P\n"`; do echo "$$file "; done;)
4+
5+
16
.PHONY: build
27
build: cs-fix test
38

@@ -7,6 +12,12 @@ cs-fix:
712

813
.PHONY: test
914
test:
10-
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
15+
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage --display-deprecations
16+
17+
.PHONY: t
18+
t:
1119

20+
all: $(targets)
1221

22+
%Test.php: t
23+
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --display-deprecations $@

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ To dump the graph, simply use the built-in dumper:
3737
$dumper = new PHPCfg\Printer\Text();
3838
echo $dumper->printScript($script);
3939
```
40+
41+
## CLI
42+
43+
You can leverage the CLI binary to generate debug traces of the CFG for any file, or for printing GraphViz visualizations.
44+
45+
```shell
46+
bin/php-cfg dot -o output.dot path/to/file.php
47+
```

bin/php-cfg

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env php
2+
<?php
3+
require_once(__DIR__ . '/../vendor/autoload.php');
4+
5+
// Init App with name and version
6+
$app = new Ahc\Cli\Application('PHP-CFG', 'v0.0.1');
7+
8+
$app->add(new PHPCfg\Cli\PrintCommand, 'p');
9+
$app->add(new PHPCfg\Cli\DotCommand, 'd');
10+
11+
$app->add(new PHPCfg\Cli\RunTestCommand);
12+
13+
$app->handle($_SERVER['argv']); // if argv[1] is `i` or `init` it executes InitCommand

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111
"require": {
1212
"php": ">=8.1",
1313
"nikic/php-parser": "^5.0",
14-
"phpdocumentor/graphviz": "^1.0.4"
14+
"phpdocumentor/graphviz": "^1.0.4",
15+
"adhocore/cli": "^v1.0.0"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": ">=10.0",
1819
"friendsofphp/php-cs-fixer": ">=3.86"
1920
},
2021
"autoload": {
2122
"psr-4": {
22-
"PHPCfg\\": "lib/PHPCfg/"
23+
"PHPCfg\\": "lib/PHPCfg/",
24+
"PHPCfg\\Cli\\": "src/Cli"
2325
}
24-
}
26+
},
27+
"bin": [
28+
"bin/php-cfg"
29+
]
2530
}

demo.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/PHPCfg/AbstractVisitor.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@
1818
*/
1919
abstract class AbstractVisitor implements Visitor
2020
{
21-
public function enterScript(Script $script) {}
21+
public function enterScript(Script $script): void {}
2222

23-
public function leaveScript(Script $script) {}
23+
public function leaveScript(Script $script): void {}
2424

25-
public function enterFunc(Func $func) {}
25+
public function enterFunc(Func $func): void {}
2626

27-
public function leaveFunc(Func $func) {}
27+
public function leaveFunc(Func $func): void {}
2828

29-
public function enterBlock(Block $block, ?Block $prior = null) {}
29+
public function enterBlock(Block $block, ?Block $prior = null): void {}
3030

31-
public function leaveBlock(Block $block, ?Block $prior = null) {}
31+
public function leaveBlock(Block $block, ?Block $prior = null): Block|int|null
32+
{
33+
return null;
34+
}
3235

33-
public function skipBlock(Block $block, ?Block $prior = null) {}
36+
public function skipBlock(Block $block, ?Block $prior = null): void {}
3437

35-
public function enterOp(Op $op, Block $block) {}
38+
public function enterOp(Op $op, Block $block): void {}
3639

37-
public function leaveOp(Op $op, Block $block) {}
40+
public function leaveOp(Op $op, Block $block): Op|int|null
41+
{
42+
return null;
43+
}
3844
}

lib/PHPCfg/Assertion.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace PHPCfg;
1313

14+
use RuntimeException;
15+
1416
class Assertion
1517
{
1618
public const MODE_NONE = 0;
@@ -19,41 +21,49 @@ class Assertion
1921

2022
public const MODE_INTERSECTION = 2;
2123

22-
public $mode = self::MODE_NONE;
24+
public readonly int $mode;
2325

2426
/**
2527
* @var Assertion[]|Operand
2628
*/
27-
public $value;
29+
public readonly array|Operand $value;
2830

29-
/**
30-
* @param Assertion[]|Operand $value
31-
*/
32-
public function __construct($value, $mode = self::MODE_NONE)
31+
public function __construct(array|Operand $value, $mode = self::MODE_NONE)
3332
{
3433
if (empty($value)) {
35-
throw new \RuntimeException('Empty value supplied for Assertion');
34+
throw new RuntimeException('Empty value supplied for Assertion');
3635
}
3736
if (is_array($value)) {
3837
foreach ($value as $v) {
3938
if (! $v instanceof self) {
40-
throw new \RuntimeException('Invalid array key supplied for Assertion');
39+
throw new RuntimeException('Invalid array key supplied for Assertion');
4140
}
4241
}
4342
if ($mode !== self::MODE_UNION && $mode !== self::MODE_INTERSECTION) {
44-
throw new \RuntimeException('Invalid mode supplied for Assertion');
43+
throw new RuntimeException('Invalid mode supplied for Assertion');
4544
}
46-
$this->mode = $mode;
47-
} elseif (! $value instanceof Operand) {
48-
throw new \RuntimeException('Invalid value supplied for Assertion: ');
45+
$this->setMode($mode);
4946
} else {
50-
$this->mode = self::MODE_NONE;
47+
$this->setMode(self::MODE_NONE);
5148
}
5249
$this->value = $value;
5350
}
5451

55-
public function getKind()
52+
public function getKind(): string
5653
{
5754
return '';
5855
}
56+
57+
protected function setMode(int $mode): void
58+
{
59+
switch ($mode) {
60+
case self::MODE_NONE:
61+
case self::MODE_UNION:
62+
case self::MODE_INTERSECTION:
63+
break;
64+
default:
65+
throw new RuntimeException("Invalid mode supplied for Assertion");
66+
}
67+
$this->mode = $mode;
68+
}
5969
}

lib/PHPCfg/Assertion/NegatedAssertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct($value)
2424
parent::__construct($value, self::MODE_INTERSECTION);
2525
}
2626

27-
public function getKind()
27+
public function getKind(): string
2828
{
2929
return 'not';
3030
}

0 commit comments

Comments
 (0)