Skip to content

Wrap in try/catch patch #229

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

Merged
merged 1 commit into from
Jun 22, 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
58 changes: 58 additions & 0 deletions src/Patch/WrapInTryCatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace PhpSchool\PhpWorkshop\Patch;

use PhpParser\Node\Stmt;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\TryCatch;

class WrapInTryCatch implements Transformer
{
/**
* @var string
*/
private $exceptionClass;

/**
* @var array<Stmt>
*/
private $statements;

/**
* @param string $exceptionClass
* @param array<Stmt>|null $statements
*/
public function __construct(string $exceptionClass = \Exception::class, array $statements = null)
{
$this->exceptionClass = $exceptionClass;
$this->statements = $statements ?: [
new Echo_([
new MethodCall(new Variable('e'), 'getMessage')
])
];
}

/**
* @param array<Stmt> $statements
* @return array<Stmt>
*/
public function transform(array $statements): array
{
return [
new TryCatch(
$statements,
[
new Catch_(
[new Name($this->exceptionClass)],
new Variable('e'),
$this->statements
)
]
)
];
}
}
55 changes: 55 additions & 0 deletions test/Patch/WrapInTryCatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace PhpSchool\PhpWorkshopTest\Patch;

use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PhpSchool\PhpWorkshop\Patch\WrapInTryCatch;
use PHPUnit\Framework\TestCase;

class WrapInTryCatchTest extends TestCase
{
public function testStatementsAreWrappedInTryCatch(): void
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse("<?php echo 'Hello World';");

$transformer = new WrapInTryCatch();
$ast = $transformer->transform($ast);

self::assertSame(
"try {\n echo 'Hello World';\n} catch (Exception \$e) {\n echo \$e->getMessage();\n}",
(new Standard())->prettyPrint($ast)
);
}

public function testStatementsAreWrappedInTryCatchWithCustomExceptionClass(): void
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse("<?php echo 'Hello World';");

$transformer = new WrapInTryCatch(\RuntimeException::class);
$ast = $transformer->transform($ast);

self::assertSame(
"try {\n echo 'Hello World';\n} catch (RuntimeException \$e) {\n echo \$e->getMessage();\n}",
(new Standard())->prettyPrint($ast)
);
}

public function testStatementsAreWrappedInTryCatchWithStatements(): void
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse("<?php echo 'Hello World';");

$transformer = new WrapInTryCatch(\RuntimeException::class, [new Echo_([new String_('You caught me!')])]);
$ast = $transformer->transform($ast);

self::assertSame(
"try {\n echo 'Hello World';\n} catch (RuntimeException \$e) {\n echo 'You caught me!';\n}",
(new Standard())->prettyPrint($ast)
);
}
}