Skip to content

Commit 813bc49

Browse files
committed
Bugfixes and Readme
1 parent 90b550b commit 813bc49

23 files changed

Lines changed: 305 additions & 114 deletions

README.md

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,100 @@
1-
# PHP Code Formatter
1+
# PHP Code Generator
22

33
[![Build Status](https://travis-ci.org/gossi/php-code-generator.svg?branch=master)](https://travis-ci.org/gossi/php-code-generator)
44
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/gossi/php-code-generator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/gossi/php-code-generator/?branch=master)
55
[![Code Coverage](https://scrutinizer-ci.com/g/gossi/php-code-generator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/gossi/php-code-generator/?branch=master)
66

7-
A library for generating php code.
7+
This library provides some tools that you commonly need for generating PHP code.
88

9-
Overview
10-
--------
9+
## Installation
1110

12-
This library provides some tools that you commonly need for generating PHP code.
11+
Install via Composer:
12+
13+
```json
14+
{
15+
"require": {
16+
"gossi/php-code-generator": "~1"
17+
}
18+
}
19+
20+
## Usage
21+
22+
There are two things you need to generate code.
23+
24+
1. A generator
25+
* CodeGenerator
26+
* CodeFileGenerator
27+
2. A model to generate
28+
* PhpClass
29+
* PhpInterface
30+
* PhpTrait
31+
* PhpFunction
32+
33+
You can create these models and push all the data using a fluent API or read from existing code through reflection.
34+
35+
### Generate Code
36+
37+
a) Simple:
38+
39+
```php
40+
use gossi\codegen\generator\CodeGenerator;
41+
use gossi\codegen\model\PhpClass;
42+
use gossi\codegen\model\PhpMethod;
43+
use gossi\codegen\model\PhpParameter;
44+
45+
$class = new PhpClass();
46+
$class->setName('my\cool\Tool');
47+
->setMethod(PhpMethod::create('__construct')
48+
->addParameter(PhpParameter::create('target')
49+
->setType('string')
50+
->setDescription('Creates my Tool')
51+
)
52+
);
53+
54+
$generator = new CodeGenerator();
55+
$code = $generator->generate($class);
56+
```
57+
58+
b) From Reflection:
59+
60+
```php
61+
use gossi\codegen\generator\CodeGenerator;
62+
use gossi\codegen\model\PhpClass;
63+
64+
$class = PhpClass::fromReflection(new \ReflectionClass('MyClass'));
65+
66+
$generator = new CodeGenerator();
67+
$code = $generator->generate($class);
68+
```
69+
70+
### Code Generators
71+
72+
**CodeGenerator**
73+
74+
Creates code for a given model
75+
76+
| Key | Type | Default Value | Description |
77+
| --- | ---- | ------------- | ----------- |
78+
| generateDocblock | boolean | true | enables docblock generation prior to code generation |
79+
| generateEmptyDocblock | boolean | true | when docblock generation is enabled, even empty docblocks will be generated |
80+
81+
**CodeFileGenerator**
82+
83+
Creates a complete php file with the given model inside.
84+
85+
Same options as `CodeGenerator` plus:
86+
87+
| Key | Type | Default Value | Description |
88+
| --- | ---- | ------------- | ----------- |
89+
| headerComment | string | empty | A comment, that will be put after the <?php statement |
90+
| headerDocblock | string\|Docblock | empty | A docblock that will be positioned after the possible header comment |
91+
| blankLineAtEnd | boolean | true | Places an empty line at the end of the generated file |
92+
93+
## Contributing
94+
95+
Feel free to fork and submit a pull request (don't forget the tests) and I am happy to merge.
96+
97+
98+
## References
1399

100+
- This project is a spin-off of the older [schmittjoh/cg-library](https://github.com/schmittjoh/cg-library) library.

src/model/AbstractPhpMember.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use gossi\codegen\model\parts\NameTrait;
2121
use gossi\codegen\model\parts\LongDescriptionTrait;
2222
use gossi\codegen\model\parts\TypeTrait;
23+
use gossi\docblock\Docblock;
2324

2425
/**
2526
* Abstract PHP member class.
@@ -47,6 +48,7 @@ abstract class AbstractPhpMember extends AbstractModel implements DocblockInterf
4748

4849
public function __construct($name) {
4950
$this->setName($name);
51+
$this->docblock = new Docblock();
5052
}
5153

5254
/**

src/model/AbstractPhpStruct.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
*/
1717
namespace gossi\codegen\model;
1818

19-
use gossi\docblock\DocBlock;
19+
use gossi\docblock\Docblock;
2020
use gossi\codegen\model\parts\QualifiedNameTrait;
2121
use gossi\codegen\model\parts\DocblockTrait;
2222
use gossi\codegen\model\parts\LongDescriptionTrait;
23+
use Doctrine\Common\Annotations\PhpParser;
2324

2425
/**
2526
* Represents an abstract php struct.
@@ -63,9 +64,17 @@ protected static function createMethod(\ReflectionMethod $method) {
6364
protected static function createProperty(\ReflectionProperty $property) {
6465
return PhpProperty::fromReflection($property);
6566
}
67+
68+
protected static function getUseStatementsFromReflection(\ReflectionClass $ref) {
69+
if (null === self::$phpParser) {
70+
self::$phpParser = new PhpParser();
71+
}
72+
return self::$phpParser->parseClass($ref);
73+
}
6674

6775
public function __construct($name = null) {
6876
$this->setQualifiedName($name);
77+
$this->docblock = new Docblock();
6978
}
7079

7180
public function setRequiredFiles(array $files) {
@@ -86,7 +95,7 @@ public function addRequiredFile($file) {
8695

8796
public function setUseStatements(array $useStatements) {
8897
$this->useStatements = $useStatements;
89-
98+
9099
return $this;
91100
}
92101

@@ -121,28 +130,28 @@ public function hasUseStatement($qualifiedName) {
121130
return isset($flipped[$qualifiedName]);
122131
}
123132

124-
public function declareUses()
125-
{
126-
foreach (func_get_args() as $name) {
127-
$this->declareUse($name);
128-
}
129-
}
133+
// public function declareUses()
134+
// {
135+
// foreach (func_get_args() as $name) {
136+
// $this->declareUse($name);
137+
// }
138+
// }
130139

131-
/**
132-
* @param string $fullClassName
133-
* @param null|string $alias
134-
*
135-
* @return string
136-
*/
137-
public function declareUse($fullClassName, $alias = null)
138-
{
139-
$fullClassName = trim($fullClassName, '\\');
140-
if (!$this->hasUseStatement($fullClassName)) {
141-
$this->addUseStatement($fullClassName, $alias);
142-
}
140+
// /**
141+
// * @param string $fullClassName
142+
// * @param null|string $alias
143+
// *
144+
// * @return string
145+
// */
146+
// public function declareUse($fullClassName, $alias = null)
147+
// {
148+
// $fullClassName = trim($fullClassName, '\\');
149+
// if (!$this->hasUseStatement($fullClassName)) {
150+
// $this->addUseStatement($fullClassName, $alias);
151+
// }
143152

144-
return $this->getUseAlias($fullClassName);
145-
}
153+
// return $this->getUseAlias($fullClassName);
154+
// }
146155

147156
/**
148157
* @param string $qualifiedName

src/model/PhpClass.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ public static function fromReflection(\ReflectionClass $ref) {
2626
$class->setQualifiedName($ref->name)
2727
->setAbstract($ref->isAbstract())
2828
->setFinal($ref->isFinal())
29-
->setConstants($ref->getConstants());
30-
31-
if (null === self::$phpParser) {
32-
self::$phpParser = new PhpParser();
33-
}
34-
$class->setUseStatements(self::$phpParser->parseClass($ref));
29+
->setConstants($ref->getConstants())
30+
->setUseStatements(static::getUseStatementsFromReflection($ref));
3531

3632
if ($ref->getDocComment()) {
3733
$docblock = new Docblock($ref);
@@ -54,6 +50,9 @@ public static function fromReflection(\ReflectionClass $ref) {
5450
foreach ($ref->getTraits() as $trait) {
5551
$class->addTrait(PhpTrait::fromReflection($trait));
5652
}
53+
54+
// constants
55+
$class->setConstants($ref->getConstants());
5756

5857
return $class;
5958
}
@@ -78,7 +77,6 @@ public function setParentClassName($name) {
7877

7978
public function generateDocblock() {
8079
parent::generateDocblock();
81-
// $docblock = $this->getDocblock();
8280

8381
foreach ($this->constants as $constant) {
8482
$constant->generateDocblock();
@@ -87,10 +85,6 @@ public function generateDocblock() {
8785
foreach ($this->properties as $prop) {
8886
$prop->generateDocblock();
8987
}
90-
91-
// $this->setDocblock($docblock);
92-
93-
// return $docblock;
9488
}
9589

9690
}

src/model/PhpConstant.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,10 @@ public static function create($name = null, $value = null) {
2424
return $constant;
2525
}
2626

27-
public static function fromReflection(\Reflection $ref) {
28-
$constant = new static($ref->name);
29-
$constant->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
30-
31-
$docblock = new Docblock($ref);
32-
$constant->setDocblock($docblock);
33-
$constant->setDescription($docblock->getShortDescription());
34-
35-
return $constant;
36-
}
37-
3827
public function __construct($name = null, $value = null) {
3928
$this->setName($name);
4029
$this->setValue($value);
30+
$this->docblock = new Docblock();
4131
}
4232

4333
public function setValue($value) {

src/model/PhpFunction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static function create($name = null) {
6969

7070
public function __construct($name = null) {
7171
$this->setQualifiedName($name);
72+
$this->docblock = new Docblock();
7273
}
7374

7475
public function generateDocblock() {

src/model/PhpInterface.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ class PhpInterface extends AbstractPhpStruct implements GenerateableInterface, C
1313
public static function fromReflection(\ReflectionClass $ref) {
1414
$interface = new static();
1515
$interface->setQualifiedName($ref->name)
16-
->setConstants($ref->getConstants());
16+
->setConstants($ref->getConstants())
17+
->setUseStatements(static::getUseStatementsFromReflection($ref));
1718

18-
$interface->setUseStatements(self::$phpParser->parseClass($ref));
19-
20-
if ($ref->getDocComment()) {
21-
$docblock = new Docblock($ref);
22-
$interface->setDocblock($docblock);
23-
$interface->setDescription($docblock->getShortDescription());
24-
$interface->setLongDescription($docblock->getLongDescription());
25-
}
19+
$docblock = new Docblock($ref);
20+
$interface->setDocblock($docblock);
21+
$interface->setDescription($docblock->getShortDescription());
22+
$interface->setLongDescription($docblock->getLongDescription());
2623

2724
foreach ($ref->getMethods() as $method) {
28-
$interface->setMethod(static::createMethod($method));
25+
$method = static::createMethod($method);
26+
$method->setAbstract(false);
27+
$interface->setMethod($method);
2928
}
3029

3130
return $interface;
@@ -41,9 +40,5 @@ public function generateDocblock() {
4140
foreach ($this->constants as $constant) {
4241
$constant->generateDocblock();
4342
}
44-
45-
// $this->setDocblock($docblock);
46-
47-
// return $docblock;
4843
}
4944
}

src/model/PhpMethod.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@ public static function create($name) {
4949

5050
public static function fromReflection(\ReflectionMethod $ref) {
5151
$method = new static($ref->name);
52-
$method->setFinal($ref->isFinal())->setAbstract($ref->isAbstract())->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref));
53-
54-
if ($ref->getDocComment()) {
55-
$docblock = new Docblock($ref);
56-
$method->setDocblock($docblock);
57-
$method->setDescription($docblock->getShortDescription());
58-
$method->setLongDescription($docblock->getLongDescription());
59-
}
52+
$method->setFinal($ref->isFinal())
53+
->setAbstract($ref->isAbstract())
54+
->setStatic($ref->isStatic())
55+
->setVisibility($ref->isPublic()
56+
? self::VISIBILITY_PUBLIC
57+
: ($ref->isProtected()
58+
? self::VISIBILITY_PROTECTED
59+
: self::VISIBILITY_PRIVATE))
60+
->setReferenceReturned($ref->returnsReference())
61+
->setBody(ReflectionUtils::getFunctionBody($ref));
62+
63+
$docblock = new Docblock($ref);
64+
$method->setDocblock($docblock);
65+
$method->setDescription($docblock->getShortDescription());
66+
$method->setLongDescription($docblock->getLongDescription());
6067

6168
foreach ($ref->getParameters() as $param) {
6269
$method->addParameter(static::createParameter($param));
@@ -85,9 +92,5 @@ public function generateDocblock() {
8592
foreach ($this->parameters as $param) {
8693
$docblock->appendTag($param->getDocblockTag());
8794
}
88-
89-
// $this->setDocblock($docblock);
90-
91-
// return $docblock;
9295
}
9396
}

src/model/PhpProperty.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ public static function create($name) {
3939

4040
public static function fromReflection(\ReflectionProperty $ref) {
4141
$property = new static($ref->name);
42-
$property->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
43-
44-
if ($ref->getDocComment()) {
45-
$docblock = new Docblock($ref);
46-
$property->setDocblock($docblock);
47-
$property->setDescription($docblock->getShortDescription());
48-
}
42+
$property->setStatic($ref->isStatic())
43+
->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
44+
45+
$docblock = new Docblock($ref);
46+
$property->setDocblock($docblock);
47+
$property->setDescription($docblock->getShortDescription());
4948

5049
$defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
5150
if (isset($defaultProperties[$ref->name])) {

0 commit comments

Comments
 (0)