Skip to content

Commit 37b59ed

Browse files
committed
Merge branch 'release/0.0.12'
2 parents 7069826 + 9bc2c4b commit 37b59ed

11 files changed

+55
-96
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
0.0.12
5+
-----
6+
Refactoring : Use statements and Namespace are contained by a file not a class as each element should only knows what it contains not what that is around itself.
7+
48
0.0.11
59
-----
610
Issue : allow backslash within class name for namespace

Component/PhpClass.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
class PhpClass extends AbstractComponent
1010
{
11-
/**
12-
* @var array
13-
*/
14-
protected $uses = array();
15-
/**
16-
* @var string
17-
*/
18-
protected $namespace = '';
1911
/**
2012
* @param string $name
2113
* @param bool $abstract
@@ -26,17 +18,6 @@ public function __construct($name, $abstract = false, $extends = null, array $in
2618
{
2719
$this->setMainElement(new PhpClassElement($name, $abstract, $extends, $interfaces));
2820
}
29-
/**
30-
* @param string $use
31-
* @param string $as
32-
* @return PhpClass
33-
*/
34-
public function addUse($use, $as = null)
35-
{
36-
$expression = empty($as) ? "use %1\$s;%3\$s" : "use %1\$s as %2\$s;%3\$s";
37-
$this->uses[] = sprintf($expression, $use, $as, self::BREAK_LINE_CHAR);
38-
return $this;
39-
}
4021
/**
4122
* @param PhpMethodElement $method
4223
* @return PhpClass
@@ -61,15 +42,6 @@ public function addMethod($name, array $parameters = array(), $access = PhpMetho
6142
{
6243
return $this->addMethodElement(new PhpMethodElement($name, $parameters, $access, $abstract, $static, $final, $hasBody));
6344
}
64-
/**
65-
* @param string $namespace
66-
* @return PhpClass
67-
*/
68-
public function setNamespace($namespace)
69-
{
70-
$this->namespace = sprintf("namespace %s;%s", $namespace, self::BREAK_LINE_CHAR);
71-
return $this;
72-
}
7345
/**
7446
* @param PhpPropertyElement $property
7547
* @return PhpClass
@@ -92,22 +64,12 @@ public function addProperty($name, $value = null, $access = PhpPropertyElement::
9264
}
9365
/**
9466
* @see \WsdlToPhp\PhpGenerator\Component\AbstractComponent::getElements()
95-
* @return AbstractElement[]|string[]
67+
* @return PhpClassElement[]
9668
*/
9769
public function getElements()
9870
{
99-
$elements = array();
100-
if (!empty($this->namespace)) {
101-
$elements[] = $this->namespace;
102-
$elements[] = self::BREAK_LINE_CHAR;
103-
}
104-
if (!empty($this->uses)) {
105-
foreach ($this->uses as $use) {
106-
$elements[] = $use;
107-
}
108-
$elements[] = self::BREAK_LINE_CHAR;
109-
}
110-
$elements[] = $this->mainElement;
111-
return $elements;
71+
return array(
72+
$this->getMainElement(),
73+
);
11274
}
11375
}

Component/PhpFile.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,25 @@ public function addFunction($name, array $parameters = array())
8383
{
8484
return $this->addFunctionElement(new PhpFunctionElement($name, $parameters));
8585
}
86+
/**
87+
* @param string $use
88+
* @param string $as
89+
* @param string $last
90+
* @return PhpFile
91+
*/
92+
public function addUse($use, $as = null, $last = false)
93+
{
94+
$expression = empty($as) ? "use %1\$s;%3\$s" : "use %1\$s as %2\$s;%3\$s";
95+
$this->mainElement->addChild(sprintf($expression, $use, $as, $last ? self::BREAK_LINE_CHAR : ''));
96+
return $this;
97+
}
98+
/**
99+
* @param string $namespace
100+
* @return PhpFile
101+
*/
102+
public function setNamespace($namespace)
103+
{
104+
$this->mainElement->addChild(sprintf("%2\$snamespace %s;%s", $namespace, self::BREAK_LINE_CHAR));
105+
return $this;
106+
}
86107
}

Component/README.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ Using one of these components, you can generate the content of more complex elem
1212
```php
1313
$class = new PhpClassComponent('Foo', true, 'stdClass');
1414
$class
15-
->setNamespace('My\\Testing\\NamespaceName')
16-
->addUse('My\\Testing\\ParentNamespace\\Model')
17-
->addUse('My\\Testing\\ParentNamespace\\Repository')
18-
->addUse('My\\Testing\\ParentNamespace\\Generator')
19-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
2015
->addAnnotationBlock('@var string')
2116
->addConstant('FOO', 'theValue')
2217
->addAnnotationBlock('@var string')
@@ -47,13 +42,6 @@ echo $class->toString();
4742
```
4843
displays
4944
```php
50-
namespace My\Testing\NamespaceName;
51-
52-
use My\Testing\ParentNamespace\Model;
53-
use My\Testing\ParentNamespace\Repository;
54-
use My\Testing\ParentNamespace\Generator;
55-
use My\Testing\ParentNamespace\Foo as FooType;
56-
5745
abstract class Foo extends stdClass
5846
{
5947
/**
@@ -95,11 +83,6 @@ abstract class Foo extends stdClass
9583
$file = new PhpFileComponent('Foo');
9684
$class = new PhpClassComponent('Foo', true, 'stdClass');
9785
$class
98-
->setNamespace('My\\Testing\\NamespaceName')
99-
->addUse('My\\Testing\\ParentNamespace\\Model')
100-
->addUse('My\\Testing\\ParentNamespace\\Repository')
101-
->addUse('My\\Testing\\ParentNamespace\\Generator')
102-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
10386
->addAnnotationBlock('@var string')
10487
->addConstant('FOO', 'theValue')
10588
->addAnnotationBlock('@var string')
@@ -126,12 +109,19 @@ $class
126109
new PhpFunctionParameterElement('uselessParameter', null),
127110
'unusedParameter'
128111
));
129-
$file->addClassComponent($class);
112+
$file
113+
->setNamespace('My\\Testing\\NamespaceName')
114+
->addUse('My\\Testing\\ParentNamespace\\Model')
115+
->addUse('My\\Testing\\ParentNamespace\\Repository')
116+
->addUse('My\\Testing\\ParentNamespace\\Generator')
117+
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
118+
->addClassComponent($class);
130119
echo $file->toString();
131120
```
132121
displays
133122
```php
134123
<?php
124+
135125
namespace My\Testing\NamespaceName;
136126

137127
use My\Testing\ParentNamespace\Model;

Tests/Component/PhpClassTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ public function testSimpleToString()
1414
$class = new PhpClassComponent('Foo', true, 'stdClass');
1515

1616
$class
17-
->setNamespace('My\\Testing\\NamespaceName')
18-
->addUse('My\\Testing\\ParentNamespace\\Model')
19-
->addUse('My\\Testing\\ParentNamespace\\Repository')
20-
->addUse('My\\Testing\\ParentNamespace\\Generator')
21-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
2217
->addAnnotationBlock('@var string')
2318
->addConstant('FOO', 'theValue')
2419
->addAnnotationBlock('@var string')

Tests/Component/PhpFileTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public function testSimpleClassToString()
1717
$class = new PhpClassComponent('Foo', true, 'stdClass');
1818

1919
$class
20-
->setNamespace('My\\Testing\\NamespaceName')
21-
->addUse('My\\Testing\\ParentNamespace\\Model')
22-
->addUse('My\\Testing\\ParentNamespace\\Repository')
23-
->addUse('My\\Testing\\ParentNamespace\\Generator')
24-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
2520
->addAnnotationBlock('@var string')
2621
->addConstant('FOO', 'theValue')
2722
->addAnnotationBlock('@var string')
@@ -48,7 +43,14 @@ public function testSimpleClassToString()
4843
new PhpFunctionParameterElement('uselessParameter', null),
4944
'unusedParameter'
5045
));
51-
$file->addClassComponent($class);
46+
47+
$file
48+
->setNamespace('My\\Testing\\NamespaceName')
49+
->addUse('My\\Testing\\ParentNamespace\\Model')
50+
->addUse('My\\Testing\\ParentNamespace\\Repository')
51+
->addUse('My\\Testing\\ParentNamespace\\Generator')
52+
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType', true)
53+
->addClassComponent($class);
5254

5355
$this->assertSameContent(__FUNCTION__, $file);
5456
}
@@ -90,11 +92,6 @@ public function testSimpleInterfaceToString()
9092
));
9193

9294
$interface
93-
->setNamespace('My\\Testing\\NamespaceName')
94-
->addUse('My\\Testing\\ParentNamespace\\Model')
95-
->addUse('My\\Testing\\ParentNamespace\\Repository')
96-
->addUse('My\\Testing\\ParentNamespace\\Generator')
97-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
9895
->addAnnotationBlock('@var string')
9996
->addConstant('FOO', 'theValue')
10097
->addAnnotationBlock('@var string')
@@ -121,7 +118,14 @@ public function testSimpleInterfaceToString()
121118
new PhpFunctionParameterElement('uselessParameter', null),
122119
'unusedParameter'
123120
));
124-
$file->addInterfaceComponent($interface);
121+
122+
$file
123+
->setNamespace('My\\Testing\\NamespaceName')
124+
->addUse('My\\Testing\\ParentNamespace\\Model')
125+
->addUse('My\\Testing\\ParentNamespace\\Repository')
126+
->addUse('My\\Testing\\ParentNamespace\\Generator')
127+
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType', true)
128+
->addInterfaceComponent($interface);
125129

126130
$this->assertSameContent(__FUNCTION__, $file);
127131
}

Tests/Component/PhpInterfaceTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public function testSimpleToString()
1717
));
1818

1919
$interface
20-
->setNamespace('My\\Testing\\NamespaceName')
21-
->addUse('My\\Testing\\ParentNamespace\\Model')
22-
->addUse('My\\Testing\\ParentNamespace\\Repository')
23-
->addUse('My\\Testing\\ParentNamespace\\Generator')
24-
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
2520
->addAnnotationBlock('@var string')
2621
->addConstant('FOO', 'theValue')
2722
->addAnnotationBlock('@var string')

Tests/resources/PhpClassTest_SimpleToString.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
namespace My\Testing\NamespaceName;
2-
3-
use My\Testing\ParentNamespace\Model;
4-
use My\Testing\ParentNamespace\Repository;
5-
use My\Testing\ParentNamespace\Generator;
6-
use My\Testing\ParentNamespace\Foo as FooType;
7-
81
abstract class Foo extends stdClass
92
{
103
/**

Tests/resources/PhpFileTest_SimpleClassToString.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace My\Testing\NamespaceName;
34

45
use My\Testing\ParentNamespace\Model;

Tests/resources/PhpFileTest_SimpleInterfaceToString.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace My\Testing\NamespaceName;
34

45
use My\Testing\ParentNamespace\Model;

0 commit comments

Comments
 (0)