Skip to content

Commit

Permalink
Improve exception message of ExtractionException
Browse files Browse the repository at this point in the history
  • Loading branch information
pierallard committed Jun 20, 2017
1 parent b28567f commit ce26755
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function it_is_initializable()
$this->shouldHaveType('Akeneo\CouplingDetector\NodeParser\PhpClass\ClassNameExtractor');
}

function it_extracts_the_class_name()
function it_extracts_the_class_name(\SplFileInfo $file)
{
$content = <<<EOF
<?php
Expand All @@ -35,11 +35,13 @@ interface FamilyInterface extends TranslatableInterface, ReferableInterface, Ver
}
EOF;
$tokens = Tokens::fromCode($content);
$this->extract($tokens)->shouldReturn('FamilyInterface');
$this->extract($tokens, $file)->shouldReturn('FamilyInterface');
}

function it_throws_an_exception_when_class_name_cannot_be_extracted(Tokens $tokens)
function it_throws_an_exception_when_class_name_cannot_be_extracted(Tokens $tokens, \SplFileInfo $file)
{
$this->shouldThrow('Akeneo\CouplingDetector\NodeParser\ExtractionException')->duringExtract($tokens);
$this
->shouldThrow('Akeneo\CouplingDetector\NodeParser\ExtractionException')
->duringExtract($tokens, $file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function it_is_initializable()
$this->shouldHaveType('Akeneo\CouplingDetector\NodeParser\PhpClass\NamespaceExtractor');
}

function it_extracts_the_class_namespace()
function it_extracts_the_class_namespace(\SplFileInfo $file)
{
$content = <<<EOF
<?php
Expand All @@ -35,11 +35,13 @@ interface FamilyInterface extends TranslatableInterface, ReferableInterface, Ver
}
EOF;
$tokens = Tokens::fromCode($content);
$this->extract($tokens)->shouldReturn('Pim\Bundle\CatalogBundle\Model');
$this->extract($tokens, $file)->shouldReturn('Pim\Bundle\CatalogBundle\Model');
}

function it_throws_an_exception_when_class_name_cannot_be_extracted(Tokens $tokens)
function it_throws_an_exception_when_class_name_cannot_be_extracted(Tokens $tokens, \SplFileInfo $file)
{
$this->shouldThrow('Akeneo\CouplingDetector\NodeParser\ExtractionException')->duringExtract($tokens);
$this
->shouldThrow('Akeneo\CouplingDetector\NodeParser\ExtractionException')
->duringExtract($tokens, $file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
class ClassNameExtractor
{
/**
* @param Tokens $tokens
* @param Tokens $tokens
* @param \SplFileInfo $file
*
* @throws ExtractionException
*
* @return string
*/
public function extract(Tokens $tokens)
public function extract(Tokens $tokens, \SplFileInfo $file)
{
$classyName = null;

Expand All @@ -33,7 +34,10 @@ public function extract(Tokens $tokens)
}

if (null === $classyName) {
throw new ExtractionException('No way to parse class name of this class');
throw new ExtractionException(sprintf(
'No way to parse the namespace of the class in %s',
$file->getFilename()
));
}

return $classyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
class NamespaceExtractor
{
/**
* @param Tokens $tokens
* @param Tokens $tokens
* @param \SplFileInfo $file
*
* @throws ExtractionException
*
* @return string
*/
public function extract(Tokens $tokens)
public function extract(Tokens $tokens, \SplFileInfo $file)
{
$namespace = null;

Expand All @@ -34,7 +35,10 @@ public function extract(Tokens $tokens)
}

if (null === $namespace) {
throw new ExtractionException('No way to parse the namespace of this class');
throw new ExtractionException(sprintf(
'No way to parse the namespace of the class in %s',
$file->getFilename()
));
}

return $namespace;
Expand Down
4 changes: 2 additions & 2 deletions src/Akeneo/CouplingDetector/NodeParser/PhpClassNodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function parse(\SplFileInfo $file)

$content = file_get_contents($file->getRealPath());
$tokens = Tokens::fromCode($content);
$classNamespace = $namespaceExtractor->extract($tokens);
$className = $classNameExtractor->extract($tokens);
$classNamespace = $namespaceExtractor->extract($tokens, $file);
$className = $classNameExtractor->extract($tokens, $file);
$classFullName = sprintf('%s\%s', $classNamespace, $className);
$useDeclarations = $useDeclarationExtractor->extract($tokens);

Expand Down

0 comments on commit ce26755

Please sign in to comment.