Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.buildpath
.php_cs.cache
*.cache
phpunit.xml
composer.lock
vendor/
Expand Down
16 changes: 16 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of the php-code-generator package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license Apache-2.0
*/

$config = new phootwork\fixer\Config();
$config->getFinder()
->exclude(['fixtures', 'generated'])
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests');

return $config;
13 changes: 0 additions & 13 deletions .php_cs

This file was deleted.

54 changes: 36 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name" : "gossi/php-code-generator",
"type" : "library",
"description" : "Toolset for generating PHP code",
"license" : "Apache-2.0",
"license" : "Apache-2.0.0",
"authors" : [{
"name" : "Thomas Gossmann",
"homepage" : "http://gos.si"
Expand All @@ -25,24 +25,42 @@
}
},
"require" : {
"php" : ">=7.2",
"gossi/docblock" : "~1",
"gossi/php-code-profiles" : "dev-master",
"gossi/php-code-formatter" : "dev-master",
"symfony/options-resolver" : "^4.1.7",
"phootwork/file" : "~0",
"phootwork/tokenizer" : "~0",
"nikic/php-parser" : "^4.1.0"
"php" : ">=8.0",
"gossi/docblock" : "^3.0",
"symfony/options-resolver" : "^5.2",
"phootwork/file" : "^3.0",
"phootwork/tokenizer" : "^3.0",
"phootwork/collection": "^3.0",
"phootwork/lang": "^3.0",
"nikic/php-parser" : "^4.0",
"twig/twig": "^3.3",
"susina/twig-extensions": "^0"
},
"require-dev" : {
"phpunit/phpunit" : "^7.4.4",
"phootwork/php-cs-fixer-config" : "~0.1"
"phpunit/phpunit" : "^9.5",
"phootwork/php-cs-fixer-config" : "^0",
"psalm/phar": "^4.6"
},
"scripts" : {
"check" : "@cs",
"cs" : "php-cs-fixer fix -v --diff --dry-run",
"cs-fix" : "php-cs-fixer fix -v --diff",
"test" : "phpunit",
"api" : "sami.php update sami.php"
"scripts": {
"analytics": "vendor/bin/psalm.phar",
"check": [
"@test",
"@analytics",
"@cs-fix"
],
"coverage:html": "@test --coverage-html coverage/",
"coverage:clover": "@test --coverage-clover clover.xml",
"cs": "php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix -v --diff",
"test": "phpunit --colors=always"
},
"scripts-descriptions": {
"analytics": "Run static analysis tool",
"check": "Perform all tests and analysis, required before submitting a pull request",
"coverage:html": "Create a code coverage report in html format, into the `coverage/` directory",
"coverage:clover": "Create a code coverage report in xml format, into `clover.xml` file",
"cs": "Run code style analysis, without fixing errors",
"cs-fix": "Run code style analysis and fix errors",
"test": "Run all tests"
}
}
}
47 changes: 25 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<testsuites>
<testsuite name="Code Generator Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>


<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Code Generator Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

</phpunit>
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
47 changes: 25 additions & 22 deletions src/parser/FileParser.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);
/*
* This file is part of the php-code-generator package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license Apache-2.0
*/

namespace gossi\codegen\parser;

use gossi\codegen\parser\visitor\ParserVisitorInterface;
use phootwork\collection\Set;
use phootwork\file\exception\FileNotFoundException;
use phootwork\file\exception\FileException;
use phootwork\file\File;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use PhpParser\ParserFactory;

class FileParser extends NodeVisitorAbstract {
private Set $visitors;
private File $file;

private $visitors;
private $filename;

public function __construct($filename) {
$this->filename = $filename;
public function __construct(string $filename) {
$this->file = new File($filename);
$this->visitors = new Set();
}

public function addVisitor(ParserVisitorInterface $visitor) {
public function addVisitor(ParserVisitorInterface $visitor): self {
$this->visitors->add($visitor);

return $this;
}

public function removeVisitor(ParserVisitorInterface $visitor) {
public function removeVisitor(ParserVisitorInterface $visitor): self {
$this->visitors->remove($visitor);

return $this;
}

Expand All @@ -37,27 +45,22 @@ public function hasVisitor(ParserVisitorInterface $visitor): bool {
}

/**
* @throws FileNotFoundException
* @throws FileException If not existent or not readable file
*/
public function parse() {
$file = new File($this->filename);

if (!$file->exists()) {
throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
}

public function parse(): void {
$parser = $this->getParser();
$traverser = new NodeTraverser();
$traverser->addVisitor($this);
$traverser->traverse($parser->parse($file->read()));
$traverser->traverse($parser->parse((string) $this->file->read()));
}

private function getParser(): Parser {
$factory = new \PhpParser\ParserFactory();
return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
$factory = new ParserFactory();

return $factory->create(ParserFactory::PREFER_PHP7);
}

public function enterNode(Node $node) {
public function enterNode(Node $node): void {
foreach ($this->visitors as $visitor) {
switch ($node->getType()) {
case 'Stmt_Namespace':
Expand Down
93 changes: 54 additions & 39 deletions src/parser/PrettyPrinter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the php-code-generator package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license Apache-2.0
*/

namespace gossi\codegen\parser;

use PhpParser\Node;
Expand All @@ -9,46 +17,53 @@

class PrettyPrinter extends Standard {

/**
* Pretty prints an array of nodes (statements) and indents them optionally.
*
* @param Node[] $nodes Array of nodes
* @param bool $indent Whether to indent the printed nodes
*
* @return string Pretty printed statements
*/
protected function pStmts(array $nodes, bool $indent = true): string {
$result = '';
/**
* Pretty prints an array of nodes (statements) and indents them optionally.
*
* @param Node[] $nodes Array of nodes
* @param bool $indent Whether to indent the printed nodes
*
* @return string Pretty printed statements
*/
protected function pStmts(array $nodes, bool $indent = true): string {
$result = '';
$prevNode = null;

foreach ($nodes as $node) {
$comments = $node->getAttribute('comments', []);
if ($comments) {
$result .= "\n" . $this->pComments($comments);
if ($node instanceof Stmt\Nop) {
continue;
}
}

if ($prevNode && $prevNode->getLine() && $node->getLine()) {
$diff = $node->getLine()- $prevNode->getLine();
if ($diff > 0) {
$result .= str_repeat("\n", $diff - 1);
}
}

$result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
$prevNode = $node;
}

if ($indent) {
return preg_replace('~\n(?!$)~', "\n ", $result);
} else {
return $result;
}
}

public function pExpr_Array(Array_ $node) {
foreach ($nodes as $node) {
$comments = $node->getAttribute('comments', []);
if ($comments) {
$result .= "\n" . $this->pComments($comments);
if ($node instanceof Stmt\Nop) {
continue;
}
}

if ($prevNode && $prevNode->getEndLine() && $node->getLine()) {
$diff = $node->getLine() - $prevNode->getEndLine();
if ($diff > 0) {
$result .= str_repeat("\n", $diff - 1);
}
}

$result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
$prevNode = $node;
}

if ($indent) {
return preg_replace('~\n(?!$)~', "\n ", $result);
} else {
return $result;
}
}

/**
* @param Array_ $node
*
* @return string
*
* @psalm-suppress InvalidArgument Internal type mismatch of nikic/php-parser library
*/
public function pExpr_Array(Array_ $node): string {
return '[' . $this->pCommaSeparated($node->items) . ']';
}
}
Loading