Skip to content

Commit

Permalink
Remove todo
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilla committed Sep 11, 2020
1 parent daba903 commit d41c9d0
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\SmartObject;
use PhpParser\Node\Expr\Array_;
use Wavevision\DIServiceAnnotation\DIService;
use Wavevision\NamespaceTranslator\Exceptions\InvalidState;

/**
* @DIService(generateInject=true)
Expand All @@ -19,9 +20,11 @@ public function process(string $resource): Array_
{
$returnFinder = new ReturnFinder();
$this->traverseFileAst->process($resource, $returnFinder);
/** @var Array_ $array */
$array = $returnFinder->getReturn()->expr;
return $array;
if ($array instanceof Array_) {
return $array;
}
throw new InvalidState('Define function must return an array.');
}

}
17 changes: 14 additions & 3 deletions src/NamespaceTranslator/Loaders/TranslationClass/ReturnFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Wavevision\NamespaceTranslator\Exceptions\InvalidState;

class ReturnFinder extends NodeVisitorAbstract
{
Expand All @@ -18,21 +19,31 @@ class ReturnFinder extends NodeVisitorAbstract
*/
public function leaveNode(Node $node)
{
//todo validate single return
if ($node instanceof ClassMethod) {
if ($node->name->name !== 'define') {
throw new InvalidState("TranslationClass must contains function 'define'.");
}
if (!($node->isStatic() && $node->isPublic())) {
throw new InvalidState('Define function must be public and static.');
}
$stmts = $node->getStmts();
if (isset($stmts[0])) {
if (isset($stmts[0]) && count($stmts) === 1) {
/** @var Return_ $return */
$return = $stmts[0];
$this->return = $return;
} else {
throw new InvalidState('Define function must have exactly one statement.');
}
return NodeTraverser::STOP_TRAVERSAL;
}
}

public function getReturn(): Return_
{
return $this->return;
if (isset($this->return)) {
return $this->return;
}
throw new InvalidState('No class method found.');
}

}
3 changes: 1 addition & 2 deletions src/NamespaceTranslator/Transfer/Export/FileSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function create(string $directory, Loader $loader, string $format): Trans
/** @var SplFileInfo $file */
foreach (Finder::findFiles('*' . $suffix)->in($translationDirectory) as $file) {
$pathname = $file->getPathname();
//todo replace rtrim
$basePathname = str_replace($suffix, '', $pathname);
$basePathname = substr_replace($pathname, '', -strlen($suffix));
$translations->add(
new FileSet(
$this->fileSet($basePathname, $loader),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass;

use Nette\SmartObject;
use PhpParser\Node\Expr\Array_;
use Wavevision\NamespaceTranslator\Exceptions\InvalidState;
use Wavevision\NamespaceTranslator\Loaders\TranslationClass\InjectGetTranslationArray;
use Wavevision\NetteTests\TestCases\DIContainerTestCase;

class GetTranslationArrayTest extends DIContainerTestCase
{

use SmartObject;
use InjectGetTranslationArray;

public function test(): void
{
$this->assertInstanceOf(
Array_::class,
$this->getTranslationArray->process(__DIR__ . '/../../App/Models/Translated/Translations/Cs.php')
);
}

public function testMissingDefine(): void
{
$this->exception(__DIR__ . '/Resources/InvalidName.php', "TranslationClass must contains function 'define'.");
}

public function testMissingFlags(): void
{
$this->exception(__DIR__ . '/Resources/InvalidFlags.php', "Define function must be public and static.");
}

public function testInvalidReturn(): void
{
$this->exception(__DIR__ . '/Resources/InvalidReturn.php', "Define function must return an array.");
}

public function testInvalidBody(): void
{
$this->exception(__DIR__ . '/Resources/InvalidBody.php', "Define function must have exactly one statement.");
}

public function testNoFunction(): void
{
$this->exception(__DIR__ . '/Resources/NoMethod.php', 'No class method found.');
}

private function exception(string $file, string $message): void
{
$this->expectException(InvalidState::class);
$this->expectExceptionMessage($message);
$this->getTranslationArray->process($file);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass\Resources;

class InvalidBody
{

/**
* @return array<mixed>
*/
public static function define(): array
{
$a = 'hello';
return [$a => $a];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass\Resources;

class InvalidFlags
{

/**
* @return array<mixed>
*/
public function define(): array
{
return [];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass\Resources;

class InvalidName
{

/**
* @return array<mixed>
*/
public static function hello(): array
{
return [];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass\Resources;

class InvalidReturn
{

public static function define(): string
{
return '';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Wavevision\NamespaceTranslatorTests\Loaders\TranslationClass\Resources;

class NoMethod
{

}

0 comments on commit d41c9d0

Please sign in to comment.