Skip to content

Commit 636e7b4

Browse files
committed
Add missing node visitor for class constants
1 parent 7a528cf commit 636e7b4

File tree

5 files changed

+193
-7
lines changed

5 files changed

+193
-7
lines changed

.github/workflows/integration.yml

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: Testing PHP Code AST
2+
on: [push, pull_request]
3+
4+
jobs:
5+
tests:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
php-version:
10+
- "7.3"
11+
- "7.4"
12+
os: [ubuntu-latest]
13+
experimental: [false]
14+
include:
15+
- php-version: "8.0"
16+
os: ubuntu-latest
17+
experimental: true
18+
runs-on: ${{ matrix.os }}
19+
name: PHP ${{ matrix.php-version }} Test on ${{ matrix.os }}
20+
continue-on-error: ${{ matrix.experimental }}
21+
steps:
22+
- name: "Checkout"
23+
uses: "actions/[email protected]"
24+
25+
- name: "Install PHP"
26+
uses: "shivammathur/[email protected]"
27+
with:
28+
php-version: "${{ matrix.php-version }}"
29+
coverage: xdebug
30+
31+
- name: Get composer cache directory
32+
id: composercache
33+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
34+
35+
- name: Cache composer dependencies
36+
uses: actions/cache@v2
37+
with:
38+
path: ${{ steps.composercache.outputs.dir }}
39+
# Use composer.json for key, if composer.lock is not committed.
40+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
41+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
42+
restore-keys: ${{ runner.os }}-composer-
43+
44+
- name: Install Composer dependencies
45+
run: |
46+
composer install --no-progress --prefer-dist --optimize-autoloader
47+
48+
#- name: Run Tests
49+
# run: php vendor/bin/phpunit --coverage-text
50+
51+
coding-standard:
52+
name: Coding Standard
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Checkout
56+
uses: actions/checkout@v2
57+
58+
- name: Setup PHP
59+
uses: shivammathur/setup-php@v2
60+
with:
61+
php-version: '7.4'
62+
63+
- name: Get composer cache directory
64+
id: composercache
65+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
66+
67+
- name: Cache composer dependencies
68+
uses: actions/cache@v2
69+
with:
70+
path: ${{ steps.composercache.outputs.dir }}
71+
# Use composer.json for key, if composer.lock is not committed.
72+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
73+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
74+
restore-keys: ${{ runner.os }}-composer-
75+
76+
- name: Install dependencies
77+
run: composer install --no-progress --prefer-dist --optimize-autoloader
78+
79+
- name: PHP CodeSniffer
80+
run: composer cs
81+
82+
static-analysis:
83+
name: Static Analysis
84+
runs-on: ubuntu-latest
85+
steps:
86+
- name: Checkout
87+
uses: actions/checkout@v2
88+
89+
- name: Setup PHP
90+
uses: shivammathur/setup-php@v2
91+
with:
92+
php-version: '7.4'
93+
94+
- name: Get composer cache directory
95+
id: composercache
96+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
97+
98+
- name: Cache composer dependencies
99+
uses: actions/cache@v2
100+
with:
101+
path: ${{ steps.composercache.outputs.dir }}
102+
# Use composer.json for key, if composer.lock is not committed.
103+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
104+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
105+
restore-keys: ${{ runner.os }}-composer-
106+
107+
- name: Install dependencies
108+
run: composer install --no-progress --prefer-dist --optimize-autoloader
109+
110+
- name: Static Analysis using PHPStan
111+
run: composer analyse

src/Code/MethodGenerator.php

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace OpenCodeModeling\CodeAst\Code;
1212

1313
use OpenCodeModeling\CodeAst\Exception;
14-
use PhpParser\Node;
1514
use PhpParser\Node\Stmt\ClassMethod;
1615

1716
/**

src/Code/PropertyGenerator.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ public function __construct(
6565
}
6666
}
6767

68-
/**
69-
* @param string $type
70-
* @return ParameterGenerator
71-
*/
72-
public function setType($type): self
68+
public function setType(string $type): self
7369
{
7470
$this->type = TypeGenerator::fromTypeString($type);
7571

@@ -91,7 +87,7 @@ public function setDefaultValue(
9187
$defaultValue,
9288
$defaultValueType = ValueGenerator::TYPE_AUTO
9389
): self {
94-
if (!$defaultValue instanceof ValueGenerator) {
90+
if (! $defaultValue instanceof ValueGenerator) {
9591
$defaultValue = new ValueGenerator($defaultValue, $defaultValueType);
9692
}
9793

src/NodeVisitor/ClassConstant.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeAst\NodeVisitor;
12+
13+
use OpenCodeModeling\CodeAst\Code\IdentifierGenerator;
14+
use PhpParser\Node;
15+
use PhpParser\Node\Stmt\Class_;
16+
use PhpParser\NodeVisitorAbstract;
17+
18+
final class ClassConstant extends NodeVisitorAbstract
19+
{
20+
/**
21+
* @var IdentifierGenerator
22+
*/
23+
private $lineGenerator;
24+
25+
public function __construct(IdentifierGenerator $lineGenerator)
26+
{
27+
$this->lineGenerator = $lineGenerator;
28+
}
29+
30+
public function enterNode(Node $node)
31+
{
32+
if ($node instanceof Class_) {
33+
if ($definitions = $this->constant($node)) {
34+
$node->stmts = \array_merge(
35+
$definitions,
36+
$node->stmts
37+
);
38+
39+
return $node;
40+
}
41+
}
42+
43+
return null;
44+
}
45+
46+
private function isAlreadyDefined(
47+
string $lineIdentifier,
48+
Class_ $node
49+
): bool {
50+
$alreadyDefined = false;
51+
52+
foreach ($node->stmts as $stmt) {
53+
if (! $stmt instanceof Node\Stmt\ClassConst) {
54+
continue;
55+
}
56+
57+
if ($lineIdentifier === $stmt->consts[0]->name->name) {
58+
$alreadyDefined = true;
59+
break;
60+
}
61+
}
62+
63+
return $alreadyDefined;
64+
}
65+
66+
private function constant(Class_ $node): ?array
67+
{
68+
$isAlreadyDefined = $this->isAlreadyDefined(
69+
$this->lineGenerator->getIdentifier(),
70+
$node
71+
);
72+
73+
if ($isAlreadyDefined === false) {
74+
return $this->lineGenerator->generate();
75+
}
76+
77+
return null;
78+
}
79+
}

src/NodeVisitor/ClassFile.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function beforeTraverse(array $nodes)
4545
$this->classExists = $node->name->name === $this->classGenerator->getName();
4646
}
4747
}
48+
4849
return null;
4950
}
5051

0 commit comments

Comments
 (0)