Skip to content

Commit 729fc60

Browse files
authored
Merge pull request #229 from php-school/wrap-in-try-catch-patch
Wrap in try/catch patch
2 parents 643eafd + 315d39a commit 729fc60

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/Patch/WrapInTryCatch.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Patch;
4+
5+
use PhpParser\Node\Stmt;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PhpParser\Node\Expr\Variable;
8+
use PhpParser\Node\Name;
9+
use PhpParser\Node\Stmt\Catch_;
10+
use PhpParser\Node\Stmt\Echo_;
11+
use PhpParser\Node\Stmt\TryCatch;
12+
13+
class WrapInTryCatch implements Transformer
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $exceptionClass;
19+
20+
/**
21+
* @var array<Stmt>
22+
*/
23+
private $statements;
24+
25+
/**
26+
* @param string $exceptionClass
27+
* @param array<Stmt>|null $statements
28+
*/
29+
public function __construct(string $exceptionClass = \Exception::class, array $statements = null)
30+
{
31+
$this->exceptionClass = $exceptionClass;
32+
$this->statements = $statements ?: [
33+
new Echo_([
34+
new MethodCall(new Variable('e'), 'getMessage')
35+
])
36+
];
37+
}
38+
39+
/**
40+
* @param array<Stmt> $statements
41+
* @return array<Stmt>
42+
*/
43+
public function transform(array $statements): array
44+
{
45+
return [
46+
new TryCatch(
47+
$statements,
48+
[
49+
new Catch_(
50+
[new Name($this->exceptionClass)],
51+
new Variable('e'),
52+
$this->statements
53+
)
54+
]
55+
)
56+
];
57+
}
58+
}

test/Patch/WrapInTryCatchTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Patch;
4+
5+
use PhpParser\Node\Scalar\String_;
6+
use PhpParser\Node\Stmt\Echo_;
7+
use PhpParser\ParserFactory;
8+
use PhpParser\PrettyPrinter\Standard;
9+
use PhpSchool\PhpWorkshop\Patch\WrapInTryCatch;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class WrapInTryCatchTest extends TestCase
13+
{
14+
public function testStatementsAreWrappedInTryCatch(): void
15+
{
16+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
17+
$ast = $parser->parse("<?php echo 'Hello World';");
18+
19+
$transformer = new WrapInTryCatch();
20+
$ast = $transformer->transform($ast);
21+
22+
self::assertSame(
23+
"try {\n echo 'Hello World';\n} catch (Exception \$e) {\n echo \$e->getMessage();\n}",
24+
(new Standard())->prettyPrint($ast)
25+
);
26+
}
27+
28+
public function testStatementsAreWrappedInTryCatchWithCustomExceptionClass(): void
29+
{
30+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
31+
$ast = $parser->parse("<?php echo 'Hello World';");
32+
33+
$transformer = new WrapInTryCatch(\RuntimeException::class);
34+
$ast = $transformer->transform($ast);
35+
36+
self::assertSame(
37+
"try {\n echo 'Hello World';\n} catch (RuntimeException \$e) {\n echo \$e->getMessage();\n}",
38+
(new Standard())->prettyPrint($ast)
39+
);
40+
}
41+
42+
public function testStatementsAreWrappedInTryCatchWithStatements(): void
43+
{
44+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
45+
$ast = $parser->parse("<?php echo 'Hello World';");
46+
47+
$transformer = new WrapInTryCatch(\RuntimeException::class, [new Echo_([new String_('You caught me!')])]);
48+
$ast = $transformer->transform($ast);
49+
50+
self::assertSame(
51+
"try {\n echo 'Hello World';\n} catch (RuntimeException \$e) {\n echo 'You caught me!';\n}",
52+
(new Standard())->prettyPrint($ast)
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)