Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
bin
composer.phar
composer.lock
composer.lock
.idea/*
4 changes: 2 additions & 2 deletions spec/Argument/StringBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function it_should_use_typehint__for_object_and_name_it_after_its_type(ClassIden
{
$classIdentifier->getTypeName(Argument::any())->willReturn('ArrayObject');

$this->buildFrom(array(new \ArrayObject()))->shouldReturn('\ArrayObject $arrayObject');
$this->buildFrom(array(new \ArrayObject()))->shouldReturn('ArrayObject $arrayObject');
}

function it_should_suffix_names_when_necessary_to_avoid_name_collision(ClassIdentifier $classIdentifier)
Expand All @@ -31,7 +31,7 @@ function it_should_suffix_names_when_necessary_to_avoid_name_collision(ClassIden

$this
->buildFrom(array(new \ArrayObject(), new \ArrayObject(), 1, 2))
->shouldReturn('\ArrayObject $arrayObject1, \ArrayObject $arrayObject2, $argument1, $argument2')
->shouldReturn('ArrayObject $arrayObject1, ArrayObject $arrayObject2, $argument1, $argument2')
;
}
}
78 changes: 78 additions & 0 deletions spec/Decorator/CodeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace spec\Cjm\PhpSpec\Decorator;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CodeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Cjm\PhpSpec\Decorator\Code');
}

function it_should_add_use_namespace_for_typehinted_argument_from_spl()
{
$codeIn =<<<CODE_IN
<?php
namespace Foo;

class Foo {}
CODE_IN;

$codeOut =<<<CODE_OUT
<?php
namespace Foo;
use ArrayObject;

class Foo {}
CODE_OUT;

$this->addUseStatementForArgument('ArrayObject', $codeIn)->shouldReturn($codeOut);
}

function it_should_replace_use_statement_for_the_same_typehint()
{

$codeIn =<<<CODE_IN
<?php
namespace Foo;
use \SplFixedArray;

class Foo {}
CODE_IN;

$codeOut =<<<CODE_OUT
<?php
namespace Foo;
use \SplFixedArray;

class Foo {}
CODE_OUT;

$this->addUseStatementForArgument('\SplFixedArray', $codeIn)->shouldReturn($codeOut);
}

function it_should_add_only_one_use_statement()
{
$codeIn =<<<CODE_IN
<?php
namespace Foo;
use \SplFixedArray;

class Foo {}
CODE_IN;

$codeOut =<<<CODE_OUT
<?php
namespace Foo;
use Foo\Bar\Baz;
use \SplFixedArray;

class Foo {}
CODE_OUT;

$this->addUseStatementForArgument('Foo\Bar\Baz', $codeIn)->shouldReturn($codeOut);
}
}
26 changes: 23 additions & 3 deletions src/Argument/StringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public function __construct(ClassIdentifier $classIdentifier)
public function buildFrom($arguments)
{
$argumentStrings = array();
foreach ($arguments as $key => $argument){
foreach ($arguments as $key => $argument) {
$argumentStrings[] = $this->getTypeHint($argument).$this->getArgName($argument);
}

$allCount = array_count_values($argumentStrings);
$duplicatesCount = array_filter($allCount, function ($count) {
return 1 < $count;
Expand All @@ -49,10 +50,14 @@ private function getTypeHint($argument)
{
$typeHint = '';
if (is_object($argument)) {
$typeHint .= '\\'.$this->classIdentifier->getTypeName($argument).' ';
$typeHint = $this->classIdentifier->getTypeName($argument).' ';
}

return $typeHint;
if (is_array($parts = explode('\\', $typeHint))) {
return end($parts);
} else {
return $typeHint;
}
}

/**
Expand All @@ -70,4 +75,19 @@ private function getArgName($argument)

return '$'.lcfirst($className);
}

/**
* @param $arguments
* @return mixed
*/
public function getNamespace($arguments)
{
$namespace = [];
foreach ($arguments as $argument) {
if (is_object($argument)) {
$namespace[] = addslashes($this->classIdentifier->getTypeName($argument));
}
}
return end($namespace);
}
}
42 changes: 42 additions & 0 deletions src/Decorator/Code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Cjm\PhpSpec\Decorator;

class Code
{
/**
* @param $argumentNamespace
* @param $codeIn
*
* @return mixed
*/
public function addUseStatementForArgument($argumentNamespace, $codeIn)
{
$codeOut = $this->removeNamespace($argumentNamespace, $codeIn);
$codeOut = $this->addNamespace($argumentNamespace, $codeOut);

return $codeOut;
}

/**
* @param $argumentNamespace
* @param $codeIn
*
* @return mixed
*/
private function removeNamespace($argumentNamespace, $codeIn)
{
return preg_replace('/\nuse\s+' . addslashes($argumentNamespace) . ';/', '', $codeIn);
}

/**
* @param $argumentNamespace
* @param $codeOut
*
* @return mixed
*/
private function addNamespace($argumentNamespace, $codeOut)
{
return preg_replace('/namespace\s+(.+);/', '$0' . "\n" . 'use ' . addslashes($argumentNamespace) . ';', $codeOut);
}
}
8 changes: 7 additions & 1 deletion src/Extension/TypeHintedMethodsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cjm\PhpSpec\Argument\ClassIdentifier;
use Cjm\PhpSpec\Argument\StringBuilder;
use Cjm\PhpSpec\Decorator\Code as CodeDecorator;
use Cjm\PhpSpec\Generator\TypeHintedMethodGenerator;
use PhpSpec\Extension\ExtensionInterface;
use PhpSpec\ServiceContainer;
Expand All @@ -23,12 +24,17 @@ public function load(ServiceContainer $container)
return new StringBuilder($c->get('code_generator.generators.method.classidentifier'));
});

$container->set('code_generator.decorators.code', function ($c) {
return new CodeDecorator();
});

$container->set('code_generator.generators.method', function ($c) {
return new TypeHintedMethodGenerator(
$c->get('console.io'),
$c->get('code_generator.templates'),
null,
$c->get('code_generator.generators.method.argumentbuilder')
$c->get('code_generator.generators.method.argumentbuilder'),
$c->get('code_generator.decorators.code')
);
});
}
Expand Down
12 changes: 11 additions & 1 deletion src/Generator/TypeHintedMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Cjm\PhpSpec\Generator;

use Cjm\PhpSpec\Argument\StringBuilder;
use Cjm\PhpSpec\Decorator\Code;
use Cjm\PhpSpec\Decorator\Code as CodeDecorator;
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
use PhpSpec\CodeGenerator\TemplateRenderer;
use PhpSpec\Console\IO;
Expand All @@ -11,6 +13,8 @@

class TypeHintedMethodGenerator implements GeneratorInterface
{
/** @var CodeDecorator */
public $codeDecorator;
/**
* @var \PhpSpec\Console\IO
*/
Expand All @@ -36,13 +40,15 @@ class TypeHintedMethodGenerator implements GeneratorInterface
* @param TemplateRenderer $templates
* @param Filesystem $filesystem
* @param StringBuilder $argumentBuilder
* @param CodeDecorator $codeDecorator
*/
public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null, StringBuilder $argumentBuilder)
public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null, StringBuilder $argumentBuilder, Code $codeDecorator)
{
$this->argumentBuilder = $argumentBuilder;
$this->io = $io;
$this->templates = $templates;
$this->filesystem = $filesystem ?: new Filesystem;
$this->codeDecorator = $codeDecorator;
}

/**
Expand Down Expand Up @@ -70,6 +76,7 @@ public function generate(ResourceInterface $resource, array $data = array())
$arguments = $data['arguments'];

$argString = $this->argumentBuilder->buildFrom($arguments);
$argumentNamespace = $this->argumentBuilder->getNamespace($arguments);

$values = array('%name%' => $name, '%arguments%' => $argString);
if (!$content = $this->templates->render('method', $values)) {
Expand All @@ -79,6 +86,9 @@ public function generate(ResourceInterface $resource, array $data = array())
}

$code = $this->filesystem->getFileContents($filepath);
if ($argumentNamespace) {
$code = $this->codeDecorator->addUseStatementForArgument($argumentNamespace, $code);
}
$code = preg_replace('/}[ \n]*$/', rtrim($content) ."\n}\n", trim($code));
$this->filesystem->putFileContents($filepath, $code);

Expand Down