@@ -84,9 +84,20 @@ private function visitUseStatements(AbstractPhpStruct $struct) {
8484 if ($ useStatements = $ struct ->getUseStatements ()) {
8585 $ this ->ensureBlankLine ();
8686 foreach ($ useStatements as $ alias => $ namespace ) {
87+ if (false === strpos ($ namespace , '\\' )) {
88+ $ commonName = $ namespace ;
89+ } else {
90+ $ commonName = substr ($ namespace , strrpos ($ namespace , '\\' ) + 1 );
91+ }
92+
93+ if (false === strpos ($ namespace , '\\' ) && !$ struct ->getNamespace ()) {
94+ //avoid fatal 'The use statement with non-compound name '$commonName' has no effect'
95+ continue ;
96+ }
97+
8798 $ this ->writer ->write ('use ' . $ namespace );
88-
89- if (substr ( $ namespace , strrpos ( $ namespace , '\\' ) + 1 ) !== $ alias ) {
99+
100+ if ($ commonName !== $ alias ) {
90101 $ this ->writer ->write (' as ' . $ alias );
91102 }
92103
@@ -187,7 +198,7 @@ public function startVisitingStructConstants() {
187198 }
188199
189200 public function visitStructConstant (PhpConstant $ constant ) {
190- $ this ->writer ->writeln ('const ' . $ constant ->getName () . ' = ' . var_export ($ constant ->getValue (), true ) . '; ' );
201+ $ this ->writer ->writeln ('const ' . $ constant ->getName () . ' = ' . $ this -> getPhpExport ($ constant ->getValue ()) . '; ' );
191202 }
192203
193204 public function endVisitingStructConstants () {
@@ -203,12 +214,28 @@ public function visitProperty(PhpProperty $property) {
203214 $ this ->writer ->write ($ property ->getVisibility () . ' ' . ($ property ->isStatic () ? 'static ' : '' ) . '$ ' . $ property ->getName ());
204215
205216 if ($ property ->hasDefaultValue ()) {
206- $ this ->writer ->write (' = ' . var_export ($ property ->getDefaultValue (), true ));
217+ $ this ->writer ->write (' = ' . $ this -> getPhpExport ($ property ->getDefaultValue ()));
207218 }
208219
209220 $ this ->writer ->writeln ('; ' );
210221 }
211222
223+ protected function getPhpExport ($ value ) {
224+ if (is_bool ($ value )) {
225+ return $ value ? 'true ' : 'false ' ;
226+ }
227+
228+ if (null === $ value ) {
229+ return 'null ' ;
230+ }
231+
232+ if (is_array ($ value )) {
233+ return 'array() ' ;
234+ }
235+
236+ return var_export ($ value , true );
237+ }
238+
212239 public function endVisitingProperties () {
213240 $ this ->writer ->writeln ();
214241 }
@@ -247,7 +274,7 @@ public function visitMethod(PhpMethod $method) {
247274 return ;
248275 }
249276
250- $ this ->writer ->writeln (') { ' )->indent ()->writeln ($ method ->getBody ())->outdent ()->rtrim ()->write ("} \n\n" );
277+ $ this ->writer ->writeln (') { ' )->indent ()->writeln (trim ( $ method ->getBody () ))->outdent ()->rtrim ()->write ("} \n\n" );
251278 }
252279
253280 public function endVisitingMethods () {
@@ -280,7 +307,7 @@ public function visitFunction(PhpFunction $function) {
280307
281308 $ this ->writer ->write ("function {$ function ->getName ()}( " );
282309 $ this ->writeParameters ($ function ->getParameters ());
283- $ this ->writer ->write (") { \n" )->indent ()->writeln ($ function ->getBody ())->outdent ()->rtrim ()->write ('} ' );
310+ $ this ->writer ->write (") { \n" )->indent ()->writeln (trim ( $ function ->getBody () ))->outdent ()->rtrim ()->write ('} ' );
284311 }
285312
286313 public function getContent () {
@@ -308,11 +335,17 @@ private function writeParameters(array $parameters) {
308335 if ($ parameter ->hasDefaultValue ()) {
309336 $ this ->writer ->write (' = ' );
310337 $ defaultValue = $ parameter ->getDefaultValue ();
311-
312- if (is_array ($ defaultValue ) && empty ($ defaultValue )) {
313- $ this ->writer ->write ('array() ' );
314- } else {
315- $ this ->writer ->write (var_export ($ defaultValue , true ));
338+
339+ switch (true ) {
340+ case is_array ($ defaultValue ) && empty ($ defaultValue ):
341+ $ this ->writer ->write ('array() ' );
342+ break ;
343+ case ($ defaultValue instanceof PhpConstant):
344+ $ this ->writer ->write ($ defaultValue ->getName ());
345+ break ;
346+ default :
347+ $ this ->writer ->write ($ this ->getPhpExport ($ defaultValue ));
348+
316349 }
317350 }
318351 }
0 commit comments