Skip to content

Commit e182ea1

Browse files
committed
addSkipDirectory
Updated documentation
1 parent b3d347d commit e182ea1

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
This package will checks for easy to miss syntax errors in all your PHP code. It will also check all your classes to see if they are loadable, but without actually instantiating the class.
66

7-
Often we accidently check in code with easily detectable syntax errors, but unless the file or class is actually loaded by PHP, we might not see the error. Often the file or class is syntaxically correct, but a method signature may not match an updated vendor library. Normally this would only be detectable at run time, but with PHPUnitSyntaxCoverage, you can make sure all files and classes are checked.
7+
Often we accidently check in code with easily detectable syntax errors, but unless the file or class is actually loaded by PHP, we might not see the error. Often the file or class is syntaxically correct, but a method signature may not match an updated class or vendor library. Normally this would only be detectable at run time, but with **PHPUnitSyntaxCoverage**, you can make sure all files and classes are checked.
88

99
PHPUnitSyntaxCoverage uses [PhpParser](https://github.com/nikic/PHP-Parser) to check for basic syntax errors. It then uses [ReflectionClass](https://www.php.net/manual/en/class.reflectionclass.php) to load any classes that are found in the source without instantiating them. This will find additional errors (such as missing or changed base classes from a package update).
1010

1111
# Requirements
1212
- PHP 7.1 or higher
1313
- PHPUnit 7 or higher
14+
- Correctly configured autoloading
1415

1516
## Installation
1617
```
@@ -22,8 +23,10 @@ Extend your unit tests from \PHPFUI\PHPUnitSyntaxCoverage\Extensions
2223
```php
2324
class UnitTest extends \PHPFUI\PHPUnitSyntaxCoverage\Extensions
2425
{
25-
public function testValidPHP()
26+
public function testProjectSyntax()
2627
{
28+
$this->addSkipDirectory(__DIR__ . '/../App/Examples');
29+
$this->assertValidPHPDirectory(__DIR__ . '/../App', 'App directory has an error');
2730
$this->assertValidPHPFile(__FILE__, 'Unit Test file not valid');
2831
$this->assertValidPHP('<?php echo "hi";');
2932
}
@@ -41,9 +44,27 @@ Instead of file by file testing, use **assertValidPHPDirectory** to test an enti
4144
```
4245
The error message will include the offending file name and line number.
4346

47+
Use **addSkipDirectory** to add simple case insensitive file matching to skip specific directories / files.
48+
49+
## Autoloading
50+
You must make sure autoloading is correctly configured for all classes. This means you can't pass references to classes that will not resolve correctly in your source. Use **addSkipDirectory** if you have test code that may not validate correctly.
51+
52+
## PHP Version
53+
While this library only supports PHP 7.1 or higher, you can create a project and point it to PHP 5.2 or higher. The default is to prefer PHP 7 code, but to prefer or only parse PHP 5, configure phpunit.xml(.dist) with
54+
55+
~~~xml
56+
<php>
57+
<env name="PHPFUI\PHPUnitSyntaxCoverage\Extensions_parser_type" value="X"/>
58+
</php>
59+
~~~
60+
Where X is one of the following **numbers**:
61+
1. Prefer PHP 7
62+
2. Prefer PHP 5
63+
3. Only PHP 7
64+
4. Only PHP 5
65+
4466
## Examples
4567
See [examples](https://github.com/phpfui/PHPUnitSyntaxCoverage/blob/master/tests/UnitTest.php)
4668

4769
## Documentation
48-
4970
Full documentation at [PHPFUI\PHPUnitSyntaxCoverage](http://phpfui.com/?p=d&n=PHPFUI%5CPHPUnitSyntaxCoverage)

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=7.1"
16-
},
17-
"require-dev": {
15+
"php": ">=7.1",
1816
"nikic/php-parser": "^4.8",
1917
"phpunit/phpunit": "^7.0|^8.0|>=9.0"
2018
},

src/PHPFUI/PHPUnitSyntaxCoverage/Extensions.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ class Extensions extends \PHPUnit\Framework\TestCase implements \PHPUnit\Runner\
4141
{
4242

4343
private static $parser = null;
44+
private $skipDirectories = [];
4445

4546
public static function setUpBeforeClass() : void
4647
{
4748
$factory = new \PhpParser\ParserFactory();
48-
self::$parser = $factory->create($_ENV['parser_type'] ?? \PhpParser\ParserFactory::PREFER_PHP7);
49+
self::$parser = $factory->create($_ENV[__CLASS__ . '_parser_type'] ?? \PhpParser\ParserFactory::PREFER_PHP7);
4950
}
5051

52+
/**
53+
* Assert a string containing valid PHP will parse.
54+
*
55+
* Important: Any classes defined in this code will not be seen by the autoloader, as it only exists in this string.
56+
*/
5157
public function assertValidPHP(string $code, string $message = '') : void
5258
{
5359
$this->assertNotEmpty($code, 'Empty PHP file. ' . $message);
@@ -83,7 +89,21 @@ public function assertValidPHP(string $code, string $message = '') : void
8389
}
8490

8591
/**
86-
* Validate all files in a directory.
92+
* Exclude any file with this $directory string in the path.
93+
*
94+
* Only a simple stripos is used to match anything in the file name.
95+
*
96+
* You can add multiple skips.
97+
*/
98+
public function addSkipDirectory(string $directory) : self
99+
{
100+
$this->skipDirectories[] = $directory;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* Validate all files in a directory. Recursive and only looks at .php files by default.
87107
*/
88108
public function assertValidPHPDirectory(string $directory, string $message = '', bool $recurseSubdirectories = true, array $extensions = ['.php']) : void
89109
{
@@ -101,19 +121,34 @@ public function assertValidPHPDirectory(string $directory, string $message = '',
101121

102122
foreach ($iterator as $item)
103123
{
104-
if ('file' == $item->getType())
124+
$type = $item->getType();
125+
if ('file' == $type)
105126
{
106127
$file = $item->getPathname();
107128
$ext = strrchr($file, '.');
108-
109129
if ($ext && isset($exts[$ext]))
110130
{
111-
$this->assertValidPHPFile($file, $message . "\nFile: " . $file);
131+
$skip = false;
132+
foreach ($this->skipDirectories as $directory)
133+
{
134+
if (stripos($file, $directory) !== false)
135+
{
136+
$skip = true;
137+
break;
138+
}
139+
}
140+
if (! $skip)
141+
{
142+
$this->assertValidPHPFile($file, $message . "\nFile: " . $file);
143+
}
112144
}
113145
}
114146
}
115147
}
116148

149+
/**
150+
* Test a specific file
151+
*/
117152
public function assertValidPHPFile(string $fileName, string $message = '') : void
118153
{
119154
$this->assertFileExists($fileName, $message);

0 commit comments

Comments
 (0)