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

Commit 48947bc

Browse files
committed
Merge branch 'master' of github.com:wavevision/utils
* 'master' of github.com:wavevision/utils: Fix tokenizer matching invalid class definition Fix readme
2 parents 6a5abb8 + db17e3d commit 48947bc

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ The package contains useful classes for:
2222
- [Finder](./src/Utils/Finder.php) – adds sorting to [nette/finder](https://github.com/nette/finder)
2323
- [Json](./src/Utils/Json.php) – JSON pretty encoder with PHP and JavaScript indents
2424
- [Objects](./src/Utils/Objects.php) – dynamic get / set accessors
25-
- [Path](./src/Utils/Path.php) – join path parts**
25+
- [Path](./src/Utils/Path.php) – join path parts
2626
- [Server](./src/Utils/Server.php) – access some useful server info (e.g. file upload limit)
2727
- [Strings](./src/Utils/Strings.php) – string helpers (encode, transform etc.)
28-
- [Tokenizer](./src/Utils/Tokenizer.php) – get structure from file (e.g. a class)
28+
- [Tokenizer](./src/Utils/Tokenizer/Tokenizer.php) – get structure from file (e.g. a class)
2929
- [Validators](./src/Utils/Validators.php) – validate Czech and Slovak numbers (phone, personal, business)

src/Utils/Tokenizer/Tokenizer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@ public function getStructureNameFromFile(string $fileName, array $tokens): ?Toke
1919
{
2020
$namespace = $structure = null;
2121
$parseNamespace = $parseStructure = false;
22+
$foundStructure = false;
23+
$foundWhitespace = false;
2224
$matchedToken = null;
2325
foreach (token_get_all(FileSystem::read($fileName)) as $token) {
2426
if ($this->tokenMatchesType($token, T_NAMESPACE)) {
2527
$parseNamespace = true;
2628
}
27-
if ($this->tokenMatchesOneType($token, $tokens)) {
28-
$matchedToken = $token[0];
29-
$parseStructure = true;
30-
}
3129
if ($parseNamespace) {
3230
$this->parseNamespace($token, $namespace, $parseNamespace);
3331
}
34-
if ($parseStructure && $this->tokenMatchesType($token, T_STRING)) {
32+
if ($this->tokenMatchesOneType($token, $tokens)) {
33+
$matchedToken = $token[0];
34+
$foundStructure = true;
35+
}
36+
if ($foundStructure && $foundWhitespace && $this->tokenMatchesType($token, T_STRING)) {
3537
$structure = $token[1];
3638
break;
3739
}
40+
$foundWhitespace = $this->tokenMatchesType($token, T_WHITESPACE);
3841
}
3942
if ($structure === null) {
4043
return null;

tests/UtilsTests/TokenizerTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class TokenizerTest extends TestCase
1010
{
1111

12-
public function testGetStructureNameFromFile(): void
12+
public function testGetStructureNameFromFileValid(): void
1313
{
1414
$class = (new Tokenizer())->getStructureNameFromFile($this->getFile('<?php class Two {}'), [T_CLASS]);
1515
$this->assertEquals('Two', $class->getName());
@@ -18,10 +18,6 @@ public function testGetStructureNameFromFile(): void
1818
[T_INTERFACE]
1919
);
2020
$this->assertEquals('Two', $interface->getName());
21-
$this->assertEquals(
22-
null,
23-
(new Tokenizer())->getStructureNameFromFile($this->getFile('<?php '), [T_CLASS])
24-
);
2521
$namespace = (new Tokenizer())->getStructureNameFromFile(
2622
$this->getFile(
2723
'<?php
@@ -36,6 +32,21 @@ class Two {}
3632
$this->assertEquals(T_CLASS, $namespace->getToken());
3733
}
3834

35+
public function testGetStructureNameFromFileInvalid(): void
36+
{
37+
$this->assertNoStructure('<?php function (){};');
38+
$this->assertNoStructure('<?php X::getByType(Application::class)->run();');
39+
$this->assertNoStructure('<?php X::getByType(Application::class )->run();');
40+
}
41+
42+
private function assertNoStructure(string $php): void
43+
{
44+
$this->assertEquals(
45+
null,
46+
(new Tokenizer())->getStructureNameFromFile($this->getFile($php), [T_CLASS])
47+
);
48+
}
49+
3950
private function getFile(string $content): string
4051
{
4152
fs::setup('r');

0 commit comments

Comments
 (0)