-
Notifications
You must be signed in to change notification settings - Fork 22
Support of @mixin annotation #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64a4f53
Added support of @mixin annotation to the BuilderMethodExtension
Ascendens 6d0e01d
Added tests for BuilderMethodExtension
Ascendens 52b0925
Added support of @mixin annotation to the FacadeMethodExtension
Ascendens 8a051fb
Added tests for FacadeMethodExtension
Ascendens 5f51879
Added annotations helper
Ascendens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Weebly\PHPStan\Laravel; | ||
|
||
use PHPStan\Testing\TestCase; | ||
use PHPStan\Broker\Broker; | ||
use Weebly\PHPStan\Laravel\MethodReflectionFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPStan\Reflection\Php\PhpMethodReflectionFactory; | ||
use PHPStan\Reflection\Php\PhpMethodReflection; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Weebly\PHPStan\Laravel\BuilderMethodExtension; | ||
use stdClass; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
/** | ||
* @package Tests\Weebly\PHPStan\Laravel | ||
*/ | ||
class BuilderMethodExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @var Broker | ||
*/ | ||
private $broker; | ||
|
||
public function testHasMethodInSubclassOfModel() | ||
{ | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'find')); | ||
$this->assertTrue($this->hasMethod(ChildOfModel::class, 'find')); | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'select')); | ||
$this->assertTrue($this->hasMethod(ChildOfModel::class, 'select')); | ||
} | ||
|
||
public function testHasMethodInClassWithMixinAnnotation() | ||
{ | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'find')); | ||
$this->assertTrue($this->hasMethod(HasAMixinAnnotation::class, 'find')); | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'select')); | ||
$this->assertTrue($this->hasMethod(HasAMixinAnnotation::class, 'select')); | ||
} | ||
|
||
public function testHasMethodInBuilder() | ||
{ | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'find')); | ||
$this->assertTrue($this->hasMethod(Builder::class, 'find')); | ||
$this->assertFalse($this->hasMethod(stdClass::class, 'select')); | ||
$this->assertTrue($this->hasMethod(Builder::class, 'select')); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->broker = $this->createBroker(); | ||
} | ||
|
||
/** | ||
* @return MethodReflectionFactory | ||
*/ | ||
private function makeMethodReflectionFactoryMock() | ||
{ | ||
/** @var MockObject|PhpMethodReflectionFactory $phpMethodReflectionFactory */ | ||
$phpMethodReflectionFactory = $this | ||
->getMockBuilder(PhpMethodReflectionFactory::class) | ||
->getMockForAbstractClass(); | ||
$methodReflectionMock = $this | ||
->getMockBuilder(PhpMethodReflection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$phpMethodReflectionFactory->method('create')->willReturn($methodReflectionMock); | ||
/** @var FileTypeMapper $fileTypeMapper */ | ||
$fileTypeMapper = $this->getContainer()->createInstance(FileTypeMapper::class); | ||
|
||
return new MethodReflectionFactory($phpMethodReflectionFactory, $fileTypeMapper); | ||
} | ||
|
||
/** | ||
* Check existence of the method in given class | ||
* | ||
* @param string $className | ||
* @param string $methodName | ||
* @return bool | ||
*/ | ||
private function hasMethod(string $className, string $methodName): bool | ||
{ | ||
$extension = new BuilderMethodExtension($this->makeMethodReflectionFactoryMock()); | ||
$extension->setBroker($this->broker); | ||
|
||
return $extension->hasMethod($this->broker->getClass($className), $methodName); | ||
} | ||
} | ||
|
||
class ChildOfModel extends Model {} | ||
|
||
/** | ||
* Some description | ||
* | ||
* @mixin \Illuminate\Database\Eloquent\Builder | ||
* @method string getString() | ||
*/ | ||
class HasAMixinAnnotation {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Tests\Weebly\PHPStan\Laravel; | ||
|
||
use PHPStan\Testing\TestCase; | ||
use PHPStan\Broker\Broker; | ||
use Weebly\PHPStan\Laravel\MethodReflectionFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPStan\Reflection\Php\PhpMethodReflectionFactory; | ||
use PHPStan\Reflection\Php\PhpMethodReflection; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Weebly\PHPStan\Laravel\FacadeMethodExtension; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @package Tests\Weebly\PHPStan\Laravel | ||
*/ | ||
class FacadeMethodExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @var Broker | ||
*/ | ||
private $broker; | ||
|
||
public function testHasMethod() | ||
{ | ||
// Native accessor method | ||
$this->assertTrue($this->hasMethod(TestFacade::class, 'someMethod')); | ||
$this->assertFalse($this->hasMethod(TestFacade::class, 'fakeMethod')); | ||
// Method from accessor mixin | ||
$this->assertTrue($this->hasMethod(TestFacade::class, 'table')); | ||
$this->assertTrue($this->hasMethod(TestFacade::class, 'shouldUse')); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->broker = $this->createBroker(); | ||
} | ||
|
||
/** | ||
* @return MethodReflectionFactory | ||
*/ | ||
private function makeMethodReflectionFactoryMock() | ||
{ | ||
/** @var MockObject|PhpMethodReflectionFactory $phpMethodReflectionFactory */ | ||
$phpMethodReflectionFactory = $this | ||
->getMockBuilder(PhpMethodReflectionFactory::class) | ||
->getMockForAbstractClass(); | ||
$methodReflectionMock = $this | ||
->getMockBuilder(PhpMethodReflection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$phpMethodReflectionFactory->method('create')->willReturn($methodReflectionMock); | ||
/** @var FileTypeMapper $fileTypeMapper */ | ||
$fileTypeMapper = $this->getContainer()->createInstance(FileTypeMapper::class); | ||
|
||
return new MethodReflectionFactory($phpMethodReflectionFactory, $fileTypeMapper); | ||
} | ||
|
||
/** | ||
* Check existence of the method in given class | ||
* | ||
* @param string $className | ||
* @param string $methodName | ||
* @return bool | ||
*/ | ||
private function hasMethod(string $className, string $methodName): bool | ||
{ | ||
$extension = new FacadeMethodExtension($this->makeMethodReflectionFactoryMock()); | ||
$extension->setBroker($this->broker); | ||
|
||
return $extension->hasMethod($this->broker->getClass($className), $methodName); | ||
} | ||
} | ||
|
||
/** | ||
* @mixin \Illuminate\Database\Connection | ||
* @mixin \Illuminate\Auth\AuthManager | ||
*/ | ||
class TestFacadeAccessor { | ||
function someMethod() { | ||
return true; | ||
} | ||
} | ||
|
||
class TestFacade extends Facade { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return new TestFacadeAccessor(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make the leading slash optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twistor That
/
is the pattern delimiter!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the
\\
for the namepsace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the backslashes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twistor sorry for delay, but I have a lot of work now, but promise to you to resolve issue in next few days
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twistor I've moved mixin detection to a separate class and it will return class names without the leading slash, but if you'll try to use such form, for example, in IDE it will show incorrect namespace error (checked in PHPStorm). Of course, in IDE can be used even short class names, because it can parse use section, but in this case it is imposible to detect class namespace from doc block only.