Skip to content
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
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidPHPFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Larabuilder\Exceptions;

use Exception;

class InvalidPHPFileException extends Exception
{
public function __construct(string $filePath)
{
parent::__construct("Cannot parse PHP file: {$filePath}");
}
}
10 changes: 9 additions & 1 deletion src/PHPFileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace RonasIT\Larabuilder;

use PhpParser\Error;
use PhpParser\ParserFactory;
use RonasIT\Larabuilder\NodeTraverser;
use RonasIT\Larabuilder\Enums\AccessModifierEnum;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use RonasIT\Larabuilder\Visitors\SetPropertyValue;
use RonasIT\Larabuilder\Visitors\AddArrayPropertyItem;
use RonasIT\Larabuilder\Exceptions\InvalidPHPFileException;

class PHPFileBuilder
{
Expand All @@ -23,8 +25,14 @@ public function __construct(

$code = file_get_contents($this->filePath);

$this->syntaxTree = $parser->parse($code);
try {
$this->syntaxTree = $parser->parse($code);
} catch (Error $e) {
throw new InvalidPHPFileException($this->filePath);
}

$this->oldTokens = $parser->getTokens();

$this->traverser = new NodeTraverser();
}

Expand Down
16 changes: 14 additions & 2 deletions tests/PHPFileBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RonasIT\Larabuilder\PHPFileBuilder;
use RonasIT\Larabuilder\Enums\AccessModifierEnum;
use RonasIT\Larabuilder\Exceptions\InvalidPHPFileException;
use RonasIT\Larabuilder\Exceptions\UnexpectedPropertyTypeException;
use RonasIT\Larabuilder\Tests\Support\Traits\PHPFileBuilderTestMockTrait;

Expand Down Expand Up @@ -88,11 +89,22 @@ public function testAddArrayPropertyItemThrowsException(): void
$this->callFileGetContent('some_file_path.php', 'class_with_array_properties.php'),
);

$this->expectException(UnexpectedPropertyTypeException::class);
$this->expectExceptionMessage("Property 'notArray' has unexpected type. Expected 'array', actual 'bool'");
$this->assertExceptionThrew(UnexpectedPropertyTypeException::class, "Property 'notArray' has unexpected type. Expected 'array', actual 'bool'.");

(new PHPFileBuilder('some_file_path.php'))
->addArrayPropertyItem('notArray', 'value')
->save();
}

public function testInvalidPhpFileThrowsException(): void
{
$this->mockNativeFunction(
'RonasIT\Larabuilder',
$this->callFileGetContent('some_file_path.php', 'invalid_file.php'),
);

$this->assertExceptionThrew(InvalidPHPFileException::class, 'Cannot parse PHP file: some_file_path.php');

new PHPFileBuilder('some_file_path.php');
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/PHPFileBuilderTest/original/invalid_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

public $value = 123;