branch: externals/phpinspect commit 68ec3d0218e3d67c046df1ddad6e0d92a1a2a799 Author: Hugo Thunnissen <de...@hugot.nl> Commit: Hugo Thunnissen <de...@hugot.nl>
Return unknown-type when derived statement cannot be completely resolved --- phpinspect-resolve.el | 31 +++++++---------- test/test-buffer-indexation.el | 77 ++++++++++++++++++++++++++++++++++++++++++ test/test-resolve.el | 5 +-- 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/phpinspect-resolve.el b/phpinspect-resolve.el index 6e02abadeb..4cdc2708db 100644 --- a/phpinspect-resolve.el +++ b/phpinspect-resolve.el @@ -257,42 +257,36 @@ value/type of ->bar must be derived from the type of $foo. So (progn (pop statement) (setq type-before - (or (phpinspect-get-cached-project-typedef-method-type resolvecontext (funcall type-resolver type-before) - (cadr attribute-word)) - type-before))) + (cadr attribute-word)))) (setq type-before - (or - (phpinspect-get-cached-project-typedef-variable-type - resolvecontext - (funcall type-resolver type-before) - (cadr attribute-word)) - type-before)))))) + (phpinspect-get-cached-project-typedef-variable-type + resolvecontext + (funcall type-resolver type-before) + (cadr attribute-word))))))) ((phpinspect-static-attrib-p current-token) (let ((attribute-word (cadr current-token))) - (phpinspect--log "Found attribute word: %s" attribute-word) - (phpinspect--log "checking if next token is a list. Token: %s" - (car statement)) (when (phpinspect-word-p attribute-word) (if (phpinspect-list-p (car statement)) (progn (pop statement) (setq type-before - (or (phpinspect-get-cached-project-typedef-static-method-type resolvecontext (funcall type-resolver type-before) - (cadr attribute-word)) - type-before))))))) + (cadr attribute-word)))))))) + ((phpinspect-comment-p current-token)) ((and type-before (phpinspect-array-p current-token)) (setq type-before - (or (phpinspect--type-contains type-before) - type-before))))) + (phpinspect--type-contains type-before))) + ((t (setq type-before nil))))) (phpinspect--log "Found derived type: %s" type-before) ;; Make sure to always return a FQN - (funcall type-resolver type-before))) + (if type-before + (funcall type-resolver type-before) + phpinspect--unknown-type))) (defun phpinspect-get-variable-type-in-block (resolvecontext variable-name php-block type-resolver &optional function-arg-list) @@ -542,7 +536,6 @@ value/type." (phpinspect--get-variable-type-in-block resolvecontext (cadar expression) php-block assignments type-resolver function-arg-list))))) - (defun phpinspect-resolve-type-from-context (resolvecontext &optional type-resolver assume-derived) "Resolve the type that RESOLVECONTEXT's subject evaluates to. diff --git a/test/test-buffer-indexation.el b/test/test-buffer-indexation.el index a6140adafa..a1b583a662 100644 --- a/test/test-buffer-indexation.el +++ b/test/test-buffer-indexation.el @@ -52,5 +52,82 @@ Does not test any related functionalities." (insert "return new class() { private ?\\DateTime $d; public function __construct() {}") (phpinspect-buffer-update-project-index buffer)))) +(ert-deftest phpinspect-index-after-fix-imports-deleted-use () + (with-temp-buffer + (let ((buffer (phpinspect-claim-buffer + (current-buffer) (phpinspect--make-dummy-project)))) + (insert "<?php + +namespace Tests\\Unit; + +use App\\CIS\\Reports\\Functions\\ReferringItems; +use App\\CIS\\Reports\\ReportState; +use App\\CIS\\Reports\\TwigBuilder; +use Database\\Seeders\\TreeSeeder; +use League\\Tactician\\CommandBus; +use Tests\\TestCase; + +class ReportTest extends TestCase +{ + private ReportState $reportState; + private TreeSeeder $treeSeeder; + + private $foo; + + public function setUp(): void + { + $this->bus = resolve(CommandBus::class); + } +}") + + (phpinspect-buffer-update-project-index buffer) + + (phpinspect-fix-imports) + (phpinspect-buffer-update-project-index buffer)))) + +(ert-deftest phpinspect-index-this () + (with-temp-buffer + (let ((buffer (phpinspect-claim-buffer + (current-buffer) (phpinspect--make-dummy-project)))) + + (insert "<?php + +namespace Z; + +class A +{ + private Barry $barry; + + static function makeB(): \\B + { + } + + function nananana() + { + $this->t = self::makeB(); + $this->harry = $this->barry->getHarry(); + } +}") + (phpinspect-buffer-update-project-index buffer) + + (let ((typedef (phpinspect-project-get-typedef + (phpinspect-buffer-project buffer) + (phpinspect--make-type :name "\\Z\\A"))) + property) + + (should typedef) + (should (setq property (phpi-typedef-get-property typedef "t"))) + + (should (phpinspect--type= (phpinspect--make-type :name "\\B") + (phpi-prop-type property))) + + (should (setq property (phpi-typedef-get-property typedef "harry"))) + + (should (phpinspect--type= phpinspect--unknown-type (phpi-prop-type property))))))) + + + + + (provide 'test-buffer-indexation) ;;; test-buffer-indexation.el ends here diff --git a/test/test-resolve.el b/test/test-resolve.el index 97b9835cbf..aac4701a9d 100644 --- a/test/test-resolve.el +++ b/test/test-resolve.el @@ -193,6 +193,7 @@ result)))))) (ert-deftest phpinspect-resolve-type-from-context-static-method () + (phpinspect-purge-cache) (with-temp-buffer (insert " class Thing @@ -215,7 +216,6 @@ class Thing (project (phpinspect--make-dummy-project)) (context (phpinspect-get-resolvecontext project bmap (point)))) - (phpinspect-purge-cache) (phpinspect-project-add-index project index) (should (phpinspect--type= (phpinspect--make-type :name "\\Thing") @@ -225,6 +225,8 @@ class Thing context))))))) (ert-deftest phpinspect-resolve-type-from-context-static-method-with-preceding-words () + (phpinspect-purge-cache) + (with-temp-buffer (insert " class Thing @@ -246,7 +248,6 @@ class Thing (phpinspect-eldoc-word-width 100) (project (phpinspect--make-dummy-project)) (context (phpinspect-get-resolvecontext project bmap (point)))) - (phpinspect-purge-cache) (phpinspect-project-add-index project index) (should (phpinspect--type= (phpinspect--make-type :name "\\Thing")