Skip to content

Commit

Permalink
Merge pull request #107 from spaze/spaze/check-allowed-superglobals-i…
Browse files Browse the repository at this point in the history
…n-traits

Allow superglobal when allowed in a trait
  • Loading branch information
spaze authored Mar 29, 2022
2 parents a16e921 + 4f3cbf9 commit 8a377dc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 39 deletions.
5 changes: 2 additions & 3 deletions src/DisallowedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ private function isAllowed(Scope $scope, ?CallLike $node, DisallowedCall $disall
}
}
foreach ($disallowedCall->getAllowIn() as $allowedPath) {
$file = $scope->getTraitReflection() ? $scope->getTraitReflection()->getFileName() : $scope->getFile();
if ($file !== null && fnmatch($this->isAllowedFileHelper->absolutizePath($allowedPath), $file)) {
if ($this->isAllowedFileHelper->matches($scope, $allowedPath)) {
return $this->hasAllowedParamsInAllowed($scope, $node, $disallowedCall);
}
}
Expand Down Expand Up @@ -207,7 +206,7 @@ public function resolveType($class, Scope $scope): Type
private function isAllowedPath(Scope $scope, DisallowedConstant $disallowedConstant): bool
{
foreach ($disallowedConstant->getAllowIn() as $allowedPath) {
if (fnmatch($this->isAllowedFileHelper->absolutizePath($allowedPath), $scope->getFile())) {
if ($this->isAllowedFileHelper->matches($scope, $allowedPath)) {
return true;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/DisallowedNamespaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public function __construct(IsAllowedFileHelper $isAllowedFileHelper)
private function isAllowed(Scope $scope, DisallowedNamespace $disallowedNamespace): bool
{
foreach ($disallowedNamespace->getAllowIn() as $allowedPath) {
$match = fnmatch($this->isAllowedFileHelper->absolutizePath($allowedPath), $scope->getFile());
if ($match) {
if ($this->isAllowedFileHelper->matches($scope, $allowedPath)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedSuperglobalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(IsAllowedFileHelper $isAllowedFileHelper)
private function isAllowedPath(Scope $scope, DisallowedSuperglobal $disallowedSuperglobal): bool
{
foreach ($disallowedSuperglobal->getAllowIn() as $allowedPath) {
if (fnmatch($this->isAllowedFileHelper->absolutizePath($allowedPath), $scope->getFile())) {
if ($this->isAllowedFileHelper->matches($scope, $allowedPath)) {
return true;
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/IsAllowedFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Spaze\PHPStan\Rules\Disallowed;

use PHPStan\Analyser\Scope;
use PHPStan\File\FileHelper;

class IsAllowedFileHelper
Expand All @@ -28,7 +29,7 @@ public function __construct(FileHelper $fileHelper, ?string $allowInRootDir = nu
* @param string $path
* @return string
*/
public function absolutizePath(string $path): string
private function absolutizePath(string $path): string
{
if (strpos($path, '*') === 0) {
return $path;
Expand All @@ -40,4 +41,11 @@ public function absolutizePath(string $path): string
return $this->fileHelper->normalizePath($this->fileHelper->absolutizePath($path));
}


public function matches(Scope $scope, string $allowedPath): bool
{
$file = $scope->getTraitReflection() ? $scope->getTraitReflection()->getFileName() : $scope->getFile();
return $file !== null && fnmatch($this->absolutizePath($allowedPath), $file);
}

}
48 changes: 35 additions & 13 deletions tests/IsAllowedFileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
namespace Spaze\PHPStan\Rules\Disallowed;

use Generator;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\File\FileHelper;
use PHPUnit\Framework\TestCase;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Testing\PHPStanTestCase;
use Traits\TestClass;
use Traits\TestTrait;

class IsAllowedFileHelperTest extends TestCase
class IsAllowedFileHelperTest extends PHPStanTestCase
{

/** @var IsAllowedFileHelper */
Expand All @@ -16,23 +21,31 @@ class IsAllowedFileHelperTest extends TestCase
/** @var IsAllowedFileHelper */
private $isAllowedHelperWithRootDir;

/** @var ScopeFactory */
private $scopeFactory;

/** @var ReflectionProvider */
private $reflectionProvider;


protected function setUp(): void
{
$this->isAllowedHelper = new IsAllowedFileHelper(new FileHelper(__DIR__));
$this->isAllowedHelperWithRootDir = new IsAllowedFileHelper(new FileHelper(__DIR__), '/foo/bar');
$this->reflectionProvider = $this->createReflectionProvider();
$this->scopeFactory = $this->createScopeFactory($this->reflectionProvider, self::getContainer()->getService('typeSpecifier'));
}


/**
* @param string $input
* @param string $output
* @dataProvider pathProvider
*/
public function testAbsolutizePath(string $input, string $output, string $outputWithDir): void
public function testMatches(string $allowedPath, string $file, string $fileWithRootDir): void
{
$this->assertSame($output, $this->isAllowedHelper->absolutizePath($input));
$this->assertSame($outputWithDir, $this->isAllowedHelperWithRootDir->absolutizePath($input));
$context = ScopeContext::create($file);
$this->assertTrue($this->isAllowedHelper->matches($this->scopeFactory->create($context), $allowedPath));
$context = ScopeContext::create($fileWithRootDir);
$this->assertTrue($this->isAllowedHelperWithRootDir->matches($this->scopeFactory->create($context), $allowedPath));
}


Expand All @@ -45,18 +58,18 @@ public function pathProvider(): Generator
];
yield [
'src/*',
__DIR__ . '/src/*',
'/foo/bar/src/*',
__DIR__ . '/src/waldo.php',
'/foo/bar/src/waldo.php',
];
yield [
'../src/*',
str_replace(basename(__DIR__) . '/../', '', __DIR__ . '/../src/*'),
'/foo/src/*',
str_replace(basename(__DIR__) . '/../', '', __DIR__ . '/../src/waldo.php'),
'/foo/src/waldo.php',
];
yield [
'src/foo/../*',
__DIR__ . '/src/*',
'/foo/bar/src/*',
__DIR__ . '/src/waldo.php',
'/foo/bar/src/waldo.php',
];
yield [
'*/src',
Expand All @@ -75,4 +88,13 @@ public function pathProvider(): Generator
];
}


public function testMatchesInTraits(): void
{
$classReflection = $this->reflectionProvider->getClass(TestClass::class);
$traitReflection = $this->reflectionProvider->getClass(TestTrait::class);
$context = ScopeContext::create($classReflection->getFileName())->enterClass($classReflection)->enterTrait($traitReflection);
$this->assertTrue($this->isAllowedHelper->matches($this->scopeFactory->create($context), $traitReflection->getFileName()));
}

}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
require_once __DIR__ . '/libs/Royale.php';
require_once __DIR__ . '/libs/SomeInterface.php';
require_once __DIR__ . '/libs/Option.php';
require_once __DIR__ . '/libs/TestTrait.php';
require_once __DIR__ . '/libs/Traits.php';
23 changes: 23 additions & 0 deletions tests/libs/TestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types = 1);

namespace Traits;

trait TestTrait
{

public function x(): void
{
}


public function y(): void
{
}


public static function z(): void
{
}

}
19 changes: 0 additions & 19 deletions tests/libs/Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@

namespace Traits;

trait TestTrait
{

public function x(): void
{
}


public function y(): void
{
}


public static function z(): void
{
}

}


trait AnotherTrait
{
Expand Down

0 comments on commit 8a377dc

Please sign in to comment.