Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Fixed KernelManipulator to work with PHP short array syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
spinx committed Nov 9, 2015
1 parent e5bc3e9 commit 42d64fa
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 5 deletions.
18 changes: 14 additions & 4 deletions Manipulator/KernelManipulator.php
Expand Up @@ -59,6 +59,7 @@ public function addBundle($bundle)
}

$this->setCode(token_get_all('<?php '.implode('', $lines)), $method->getStartLine());

while ($token = $this->next()) {
// $bundles
if (T_VARIABLE !== $token[0] || '$bundles' !== $token[1]) {
Expand Down Expand Up @@ -91,17 +92,26 @@ public function addBundle($bundle)

// trim semicolon
$leadingContent = rtrim(rtrim($leadingContent), ';');

// We want to match ) & ]
$closingSymbolRegex = '#(\)|])$#';

// get closing symbol used
preg_match($closingSymbolRegex, $leadingContent, $matches);
$closingSymbol = $matches[0];

// remove last close parentheses
$leadingContent = rtrim(preg_replace('#\)$#', '', rtrim($leadingContent)));
if (substr($leadingContent, -1) !== '(') {
// end of leading content is not open parentheses, then assume that array contains at least one element
$leadingContent = rtrim(preg_replace($closingSymbolRegex, '', rtrim($leadingContent)));

if (substr($leadingContent, -1) !== '(' && substr($leadingContent, -1) !== '[' ) {
// end of leading content is not open parentheses or bracket, then assume that array contains at least one element
$leadingContent = rtrim($leadingContent, ',').',';
}

$lines = array_merge(
array($leadingContent, "\n"),
array(str_repeat(' ', 12), sprintf('new %s(),', $bundle), "\n"),
array(str_repeat(' ', 8), ');', "\n"),
array(str_repeat(' ', 8), $closingSymbol.';', "\n"),
array_slice($src, $this->line)
);

Expand Down
14 changes: 13 additions & 1 deletion Tests/Manipulator/KernelManipulatorTest.php
Expand Up @@ -60,13 +60,25 @@ public function testAddToArray($kernelOriginFilePath)
*/
public function kernelStubFilenamesProvider()
{
return array(
$stubs = array(
'With empty bundles array' => array(__DIR__.'/Stubs/EmptyBundlesKernelStub.php'),
'With empty multiline bundles array' => array(__DIR__.'/Stubs/EmptyBundlesMultilineKernelStub.php'),
'With bundles array contains comma' => array(__DIR__.'/Stubs/ContainsCommaKernelStub.php'),
'With bundles added w/o trailing comma' => array(__DIR__.'/Stubs/ContainsBundlesKernelStub.php'),
'With some extra code and bad formatted' => array(__DIR__.'/Stubs/ContainsExtraCodeKernelStub.php'),

);

if(PHP_VERSION_ID >= 50400){
$stubs = array_merge($stubs, array(
'With empty bundles array, short array syntax' => array(__DIR__.'/Stubs/EmptyBundlesShortArraySyntaxKernelStub.php'),
'With empty multiline bundles array, short array syntax' => array(__DIR__.'/Stubs/EmptyBundlesMultilineShortArraySyntaxKernelStub.php'),
'With bundles array contains comma, short array syntax' => array(__DIR__.'/Stubs/ContainsCommaShortArraySyntaxKernelStub.php'),
'With bundles added w/o trailing comma, short array syntax' => array(__DIR__.'/Stubs/ContainsBundlesShortArraySyntaxKernelStub.php'),
));
}

return $stubs;
}

/**
Expand Down
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Sensio\Bundle\GeneratorBundle\Tests\Manipulator\Stubs\StubBundle;

class ContainsBundlesShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
$bundles = [
new StubBundle(),
new StubBundle(),
new StubBundle(),
];

return $bundles;
}
}
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Sensio\Bundle\GeneratorBundle\Tests\Manipulator\Stubs\StubBundle;

class ContainsCommaShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
$bundles = [
new StubBundle(),
];

return $bundles;
}
}
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Sensio\Bundle\GeneratorBundle\Tests\Manipulator\Stubs\StubBundle;

class ContainsExtraCodeShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
$someVariable = false;

// some bad formatted definition
$bundles = [
new StubBundle(),

new StubBundle(),

];

if (!$someVariable && in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new StubBundle();
}

return $bundles;
}
}
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;

class EmptyBundlesMultilineShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
$bundles = [

];

return $bundles;
}
}
24 changes: 24 additions & 0 deletions Tests/Manipulator/Stubs/EmptyBundlesShortArraySyntaxKernelStub.php
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;

class EmptyBundlesShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
$bundles = [];

return $bundles;
}
}
33 changes: 33 additions & 0 deletions Tests/Manipulator/Stubs/UsingShortArraySyntaxKernelStub.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KernelManipulatorTest\Stubs;

use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Sensio\Bundle\GeneratorBundle\Tests\Manipulator\Stubs\StubBundle;

class UsingShortArraySyntaxKernelStub extends KernelForTest
{
public function registerBundles()
{
// short array syntax
$bundles = [
new StubBundle(),
new StubBundle(),
];

if (!$someVariable && in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new StubBundle();
}

return $bundles;
}
}

0 comments on commit 42d64fa

Please sign in to comment.