Skip to content

Commit 1036210

Browse files
committed
Initial commit
0 parents  commit 1036210

32 files changed

+2155
-0
lines changed

.docheader

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
3+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
4+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
5+
*/

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.env
2+
app.env
3+
.phpunit.result.cache
4+
build/*
5+
vendor/
6+
composer.lock
7+
.idea
8+
.php_cs.cache

.php_cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$config = new Prooph\CS\Config\Prooph();
4+
$config->getFinder()->in(__DIR__);
5+
6+
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
7+
8+
$config->setCacheFile($cacheDir . '/.php_cs.cache');
9+
10+
return $config;

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

COPYRIGHT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright (c) 2020 Sandro Keil

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2020 Sandro Keil
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# PHP Code AST
2+
3+
PHP code generation based on AST.
4+
5+
## Installation
6+
7+
```bash
8+
$ composer require open-code-modeling/php-code-ast --dev
9+
```

composer.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "open-code-modeling/php-code-ast",
3+
"description": "Open Code Modeling PHP Code AST",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Sandro Keil",
9+
"homepage": "https://sandro-keil.de",
10+
"role": "maintainer"
11+
}
12+
],
13+
"support": {
14+
"issues": "https://github.com/open-code-modeling/php-code-ast/issues",
15+
"source": "https://github.com/open-code-modeling/php-code-ast"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"OpenCodeModeling\\CodeAst\\": "src/"
20+
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"OpenCodeModelingTest\\CodeAst\\": "tests/"
25+
}
26+
},
27+
"require": {
28+
"php": "^7.3 || ^8.0",
29+
"nikic/php-parser": "^4.2"
30+
},
31+
"require-dev": {
32+
"phpspec/prophecy-phpunit": "^2.0",
33+
"phpstan/phpstan": "^0.12.33",
34+
"phpstan/phpstan-strict-rules": "^0.12.4",
35+
"phpunit/phpunit": "^9.2.6",
36+
"prooph/php-cs-fixer-config": "^0.3",
37+
"roave/security-advisories": "dev-master"
38+
},
39+
"minimum-stability": "dev",
40+
"prefer-stable": true,
41+
"scripts": {
42+
"check": [
43+
"@cs",
44+
"@docheader",
45+
"@test"
46+
],
47+
"docheader": "vendor/bin/docheader check src/ tests/",
48+
"cs": "php-cs-fixer fix src -v --diff --dry-run",
49+
"cs-fix": "php-cs-fixer fix src -v --diff",
50+
"test": "vendor/bin/phpunit",
51+
"analyse": "php vendor/bin/phpstan.phar analyse --no-interaction"
52+
},
53+
"config": {
54+
"sort-packages": true,
55+
"platform": {
56+
}
57+
},
58+
"archive": {
59+
"exclude": [
60+
".coveralls.yml",
61+
".travis.yml",
62+
"build",
63+
"phpunit.xml*",
64+
"tests"
65+
]
66+
}
67+
}

phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 3
3+
paths:
4+
- src/

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
cacheResult="true"
5+
stopOnFailure="false"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
backupGlobals="false"
10+
backupStaticAttributes="false"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
>
14+
<testsuite name="Open Code Modeling Code AST - Test Suite">
15+
<directory suffix=".php">./tests</directory>
16+
</testsuite>
17+
18+
<filter>
19+
<whitelist>
20+
<directory>./src/</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/Code/AbstractMemberGenerator.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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\Code;
12+
13+
use PhpParser\Node\Stmt\Class_;
14+
15+
/**
16+
* Code is largely lifted from the Zend\Code\Generator\AbstractMemberGenerator implementation in
17+
* Zend Code, released with the copyright and license below. It is modified to work with PHP AST.
18+
*
19+
* @see https://github.com/zendframework/zend-code for the canonical source repository
20+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
21+
* @license https://github.com/zendframework/zend-code/blob/master/LICENSE.md New BSD License
22+
*/
23+
abstract class AbstractMemberGenerator implements StatementGenerator
24+
{
25+
/**#@+
26+
* @const int Flags for construction usage
27+
*/
28+
public const FLAG_ABSTRACT = Class_::MODIFIER_ABSTRACT;
29+
public const FLAG_FINAL = Class_::MODIFIER_FINAL;
30+
public const FLAG_STATIC = Class_::MODIFIER_STATIC;
31+
public const FLAG_PUBLIC = Class_::MODIFIER_PUBLIC;
32+
public const FLAG_PROTECTED = Class_::MODIFIER_PROTECTED;
33+
public const FLAG_PRIVATE = Class_::MODIFIER_PRIVATE;
34+
/**#@-*/
35+
36+
/**#@+
37+
* @param const string
38+
*/
39+
public const VISIBILITY_PUBLIC = 'public';
40+
public const VISIBILITY_PROTECTED = 'protected';
41+
public const VISIBILITY_PRIVATE = 'private';
42+
/**#@-*/
43+
44+
/**
45+
* @var string
46+
*/
47+
protected $name;
48+
49+
/**
50+
* @var int
51+
*/
52+
protected $flags = self::FLAG_PUBLIC;
53+
54+
/**
55+
* @param int|array $flags
56+
* @return AbstractMemberGenerator
57+
*/
58+
public function setFlags($flags): self
59+
{
60+
if (\is_array($flags)) {
61+
$flagsArray = $flags;
62+
$flags = 0x00;
63+
foreach ($flagsArray as $flag) {
64+
$flags |= $flag;
65+
}
66+
}
67+
// check that visibility is one of three
68+
$this->flags = $flags;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @param int $flag
75+
* @return AbstractMemberGenerator
76+
*/
77+
public function addFlag($flag): self
78+
{
79+
$this->setFlags($this->flags | $flag);
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* @param int $flag
86+
* @return AbstractMemberGenerator
87+
*/
88+
public function removeFlag($flag): self
89+
{
90+
$this->setFlags($this->flags & ~$flag);
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param bool $isAbstract
97+
* @return AbstractMemberGenerator
98+
*/
99+
public function setAbstract($isAbstract)
100+
{
101+
return $isAbstract ? $this->addFlag(self::FLAG_ABSTRACT) : $this->removeFlag(self::FLAG_ABSTRACT);
102+
}
103+
104+
/**
105+
* @return bool
106+
*/
107+
public function isAbstract(): bool
108+
{
109+
return (bool) ($this->flags & self::FLAG_ABSTRACT);
110+
}
111+
112+
/**
113+
* @param bool $isFinal
114+
* @return AbstractMemberGenerator
115+
*/
116+
public function setFinal($isFinal)
117+
{
118+
return $isFinal ? $this->addFlag(self::FLAG_FINAL) : $this->removeFlag(self::FLAG_FINAL);
119+
}
120+
121+
/**
122+
* @return bool
123+
*/
124+
public function isFinal(): bool
125+
{
126+
return (bool) ($this->flags & self::FLAG_FINAL);
127+
}
128+
129+
/**
130+
* @param bool $isStatic
131+
* @return AbstractMemberGenerator
132+
*/
133+
public function setStatic($isStatic)
134+
{
135+
return $isStatic ? $this->addFlag(self::FLAG_STATIC) : $this->removeFlag(self::FLAG_STATIC);
136+
}
137+
138+
/**
139+
* @return bool
140+
*/
141+
public function isStatic(): bool
142+
{
143+
return (bool) ($this->flags & self::FLAG_STATIC); // is FLAG_STATIC in flags
144+
}
145+
146+
/**
147+
* @param string $visibility
148+
* @return AbstractMemberGenerator
149+
*/
150+
public function setVisibility($visibility)
151+
{
152+
switch ($visibility) {
153+
case self::VISIBILITY_PUBLIC:
154+
$this->removeFlag(self::FLAG_PRIVATE | self::FLAG_PROTECTED); // remove both
155+
$this->addFlag(self::FLAG_PUBLIC);
156+
break;
157+
case self::VISIBILITY_PROTECTED:
158+
$this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PRIVATE); // remove both
159+
$this->addFlag(self::FLAG_PROTECTED);
160+
break;
161+
case self::VISIBILITY_PRIVATE:
162+
$this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PROTECTED); // remove both
163+
$this->addFlag(self::FLAG_PRIVATE);
164+
break;
165+
}
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* @return string
172+
*/
173+
public function getVisibility(): string
174+
{
175+
switch (true) {
176+
case $this->flags & self::FLAG_PROTECTED:
177+
return self::VISIBILITY_PROTECTED;
178+
case $this->flags & self::FLAG_PRIVATE:
179+
return self::VISIBILITY_PRIVATE;
180+
default:
181+
return self::VISIBILITY_PUBLIC;
182+
}
183+
}
184+
185+
/**
186+
* @param string $name
187+
* @return AbstractMemberGenerator
188+
*/
189+
public function setName($name): self
190+
{
191+
$this->name = (string) $name;
192+
193+
return $this;
194+
}
195+
196+
/**
197+
* @return string
198+
*/
199+
public function getName(): string
200+
{
201+
return $this->name;
202+
}
203+
}

0 commit comments

Comments
 (0)