[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phpinspect 2e064d52e1 17/18: Implement containing type
From: |
ELPA Syncer |
Subject: |
[elpa] externals/phpinspect 2e064d52e1 17/18: Implement containing type inference for collection-like class properties |
Date: |
Fri, 16 Aug 2024 06:58:57 -0400 (EDT) |
branch: externals/phpinspect
commit 2e064d52e120d4cd5dab714d8ae2883ffa8de23c
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>
Implement containing type inference for collection-like class properties
---
phpinspect-index.el | 65 ++++++++++++++++++++++++++++++++---------------------
test/test-buffer.el | 16 ++++++++++---
2 files changed, 52 insertions(+), 29 deletions(-)
diff --git a/phpinspect-index.el b/phpinspect-index.el
index a806829b82..a026080522 100644
--- a/phpinspect-index.el
+++ b/phpinspect-index.el
@@ -81,6 +81,39 @@ of TYPE, if available."
(list name function-args return-type)))
+(defun phpinspect--apply-annotation-type (annotation-type-string type
type-resolver)
+ "Add/modify information of a resolved TYPE based on ANNOTATION-TYPE-STRING.
+
+Annotation type is resolved using TYPE-RESOLVER.
+"
+ (if (stringp annotation-type-string)
+ (let ((is-collection (phpinspect--type-is-collection type)))
+ (phpinspect--log "found annotation type string %s when type is %s"
+ annotation-type-string type)
+
+ (when (string-suffix-p "[]" annotation-type-string)
+ (setq is-collection t)
+ (setq annotation-type-string (string-trim-right
annotation-type-string "\\[\\]")))
+
+
+ (cond ((phpinspect--should-prefer-return-annotation type)
+ ;; Sometimes it's better to override the typehint with a more
narrow annotation
+ (setq type (funcall type-resolver
+ (phpinspect--make-type :name
annotation-type-string))))
+
+ (is-collection
+ ;; Collections need to have their "contains" slot set
+ (phpinspect--log "Detected collection type, setting contains
from annotation type '%s'"
+ annotation-type-string)
+ (setf (phpinspect--type-contains type)
+ (funcall type-resolver
+ (phpinspect--make-type :name
annotation-type-string)))
+ (setf (phpinspect--type-collection type) t))))
+ ;; else
+ (phpinspect--log "Discarding invalid annotation type %s"
annotation-type-string))
+ type)
+
+
(defun phpinspect--index-function-from-scope (type-resolver scope
comment-before &optional add-used-types namespace)
"Index a function inside SCOPE token using phpdoc metadata in COMMENT-BEFORE.
@@ -106,31 +139,8 @@ function (think \"new\" statements, return types etc.)."
;; @return annotation. When dealing with a collection, we want to store the
;; type of its members.
(let* ((return-annotation-type
- (cadadr (seq-find #'phpinspect-return-annotation-p
comment-before)))
- (is-collection
- (and type
- (phpinspect--type-is-collection type))))
- (phpinspect--log "found return annotation %s in %s when type is %s"
- return-annotation-type comment-before type)
-
- (unless (stringp return-annotation-type)
- (phpinspect--log "Discarding invalid return annotation type %s"
return-annotation-type)
- (setq return-annotation-type nil))
-
- (when return-annotation-type
- (when (string-suffix-p "[]" return-annotation-type)
- (setq is-collection t)
- (setq return-annotation-type (string-trim-right
return-annotation-type "\\[\\]")))
-
- (cond ((phpinspect--should-prefer-return-annotation type)
- (setq type (funcall type-resolver
- (phpinspect--make-type :name
return-annotation-type))))
- (is-collection
- (phpinspect--log "Detected collection type in: %s" scope)
- (setf (phpinspect--type-contains type)
- (funcall type-resolver
- (phpinspect--make-type :name
return-annotation-type)))
- (setf (phpinspect--type-collection type) t)))))
+ (cadadr (seq-find #'phpinspect-return-annotation-p
comment-before))))
+ (phpinspect--apply-annotation-type return-annotation-type type
type-resolver))
(when add-used-types
(let ((used-types (phpinspect--find-used-types-in-tokens
@@ -214,7 +224,10 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
:lifetime (when static '(:static))
:type (when type
(when add-used-types (funcall add-used-types (list type)))
- (funcall type-resolver (phpinspect--make-type :name type)))))))
+ (phpinspect--apply-annotation-type
+ (phpinspect--variable-type-string-from-comment comment-before
variable-name)
+ (funcall type-resolver (phpinspect--make-type :name type))
+ type-resolver))))))
(defun phpinspect-doc-block-p (token)
(phpinspect-token-type-p token :doc-block))
diff --git a/test/test-buffer.el b/test/test-buffer.el
index b846adcdac..087406e8ad 100644
--- a/test/test-buffer.el
+++ b/test/test-buffer.el
@@ -494,6 +494,8 @@ class AccountStatisticsController {
private Model $privModel;
static Relation $relation;
public static Relation $staticRelation;
+ /* @var Relation[] */
+ private array $relations;
}")
(phpinspect-buffer-update-project-index buffer)
@@ -509,18 +511,21 @@ class AccountStatisticsController {
(priv-model (phpinspect--class-get-variable class "privModel"))
;; Static variables are stored with "$" prefix
(relation (phpinspect--class-get-variable class "$relation"))
- (static-relation (phpinspect--class-get-variable class
"$staticRelation")))
+ (static-relation (phpinspect--class-get-variable class
"$staticRelation"))
+ (relations (phpinspect--class-get-variable class "relations")))
(should model)
(should priv-model)
(should relation)
(should static-relation)
+ (should relations)
(let ((model-type (phpinspect--make-type
:name "\\Illuminate\\Database\\Eloquent\\Model"
:fully-qualified t))
(relation-type (phpinspect--make-type
:name
"\\Illuminate\\Database\\Eloquent\\Relations\\Relation"
- :fully-qualified t)))
+ :fully-qualified t))
+ (array-type (phpinspect--make-type :name "\\array"
:fully-qualified t)))
(should (phpinspect--variable-type model))
(should (phpinspect--type= model-type (phpinspect--variable-type
model)))
@@ -529,4 +534,9 @@ class AccountStatisticsController {
(should (phpinspect--variable-type relation))
(should (phpinspect--type= relation-type
(phpinspect--variable-type relation)))
(should (phpinspect--variable-type static-relation))
- (should (phpinspect--type= relation-type
(phpinspect--variable-type static-relation)))))))))
+ (should (phpinspect--type= relation-type
(phpinspect--variable-type static-relation)))
+
+ (should (phpinspect--type= array-type (phpinspect--variable-type
relations)))
+ (should (phpinspect--type=
+ relation-type
+ (phpinspect--type-contains (phpinspect--variable-type
relations))))))))))
- [elpa] externals/phpinspect updated (3887bc58d4 -> 4ec4c40c9d), ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect d3f100388e 04/18: Resolve property types using other methods when resolving from constructor fails, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 3fff772d8a 03/18: Fix phpinspect-buffer-index-functions test, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect d141f8a6db 08/18: Infer namespace token within import manipulation loop, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 8296641ba9 07/18: Implement removal of unused imports in `phpinspect-fix-imports', ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 93b815d71a 14/18: Index used traits (juste use of type, no actual functionality outside of that), ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 6a175c9e44 06/18: Update a few doc strings in phpinspect-index.el, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 31c6e06934 09/18: Reparse buffer when fixing import (+ add comment explaining why), ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 2e064d52e1 17/18: Implement containing type inference for collection-like class properties,
ELPA Syncer <=
- [elpa] externals/phpinspect 5528aff2a2 01/18: Handle cases where the filepath of a type cannot be determined, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 2520c89680 02/18: Account for tokens after @method annotation when indexing, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect f2f1ac9b84 05/18: Add property types and anonymous function argument types to used-types index, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 130e2c06c5 13/18: Index types used in arrays, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect f1dc699560 11/18: Detect types used with "instanceoff", ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 3a0ce3ac2d 16/18: Fix another infinite recursion bug, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect b6eadfb799 12/18: Make autoload indexation more robust and prevent double indexation, ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect c20f010ee4 10/18: Introduce `phpinspect-buffer-fresh-p' and `phpinspect-buffer-reparse-if-not-fresh', ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 2d93ec7f79 15/18: Fix infinite recursion bug in `phpinspect--get-pattern-type-in-block', ELPA Syncer, 2024/08/16
- [elpa] externals/phpinspect 4ec4c40c9d 18/18: Add eldoc for sigil variables and allow display of multiple eldoc results, ELPA Syncer, 2024/08/16