Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:wavevision/utils
Browse files Browse the repository at this point in the history
* 'master' of github.com:wavevision/utils:
  Fix tokenizer matching invalid class definition
  Fix readme
  • Loading branch information
rozsival committed Oct 15, 2019
2 parents 6a5abb8 + db17e3d commit 48947bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ The package contains useful classes for:
- [Finder](./src/Utils/Finder.php) – adds sorting to [nette/finder](https://github.com/nette/finder)
- [Json](./src/Utils/Json.php) – JSON pretty encoder with PHP and JavaScript indents
- [Objects](./src/Utils/Objects.php) – dynamic get / set accessors
- [Path](./src/Utils/Path.php) – join path parts**
- [Path](./src/Utils/Path.php) – join path parts
- [Server](./src/Utils/Server.php) – access some useful server info (e.g. file upload limit)
- [Strings](./src/Utils/Strings.php) – string helpers (encode, transform etc.)
- [Tokenizer](./src/Utils/Tokenizer.php) – get structure from file (e.g. a class)
- [Tokenizer](./src/Utils/Tokenizer/Tokenizer.php) – get structure from file (e.g. a class)
- [Validators](./src/Utils/Validators.php) – validate Czech and Slovak numbers (phone, personal, business)
13 changes: 8 additions & 5 deletions src/Utils/Tokenizer/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ public function getStructureNameFromFile(string $fileName, array $tokens): ?Toke
{
$namespace = $structure = null;
$parseNamespace = $parseStructure = false;
$foundStructure = false;
$foundWhitespace = false;
$matchedToken = null;
foreach (token_get_all(FileSystem::read($fileName)) as $token) {
if ($this->tokenMatchesType($token, T_NAMESPACE)) {
$parseNamespace = true;
}
if ($this->tokenMatchesOneType($token, $tokens)) {
$matchedToken = $token[0];
$parseStructure = true;
}
if ($parseNamespace) {
$this->parseNamespace($token, $namespace, $parseNamespace);
}
if ($parseStructure && $this->tokenMatchesType($token, T_STRING)) {
if ($this->tokenMatchesOneType($token, $tokens)) {
$matchedToken = $token[0];
$foundStructure = true;
}
if ($foundStructure && $foundWhitespace && $this->tokenMatchesType($token, T_STRING)) {
$structure = $token[1];
break;
}
$foundWhitespace = $this->tokenMatchesType($token, T_WHITESPACE);
}
if ($structure === null) {
return null;
Expand Down
21 changes: 16 additions & 5 deletions tests/UtilsTests/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class TokenizerTest extends TestCase
{

public function testGetStructureNameFromFile(): void
public function testGetStructureNameFromFileValid(): void
{
$class = (new Tokenizer())->getStructureNameFromFile($this->getFile('<?php class Two {}'), [T_CLASS]);
$this->assertEquals('Two', $class->getName());
Expand All @@ -18,10 +18,6 @@ public function testGetStructureNameFromFile(): void
[T_INTERFACE]
);
$this->assertEquals('Two', $interface->getName());
$this->assertEquals(
null,
(new Tokenizer())->getStructureNameFromFile($this->getFile('<?php '), [T_CLASS])
);
$namespace = (new Tokenizer())->getStructureNameFromFile(
$this->getFile(
'<?php
Expand All @@ -36,6 +32,21 @@ class Two {}
$this->assertEquals(T_CLASS, $namespace->getToken());
}

public function testGetStructureNameFromFileInvalid(): void
{
$this->assertNoStructure('<?php function (){};');
$this->assertNoStructure('<?php X::getByType(Application::class)->run();');
$this->assertNoStructure('<?php X::getByType(Application::class )->run();');
}

private function assertNoStructure(string $php): void
{
$this->assertEquals(
null,
(new Tokenizer())->getStructureNameFromFile($this->getFile($php), [T_CLASS])
);
}

private function getFile(string $content): string
{
fs::setup('r');
Expand Down

0 comments on commit 48947bc

Please sign in to comment.