emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/phpinspect 562d5247b7 1/2: Refactor s/filename/file-nam


From: ELPA Syncer
Subject: [elpa] externals/phpinspect 562d5247b7 1/2: Refactor s/filename/file-name/g; s/typename/type-name/g + fix a bug
Date: Sat, 28 Sep 2024 12:58:50 -0400 (EDT)

branch: externals/phpinspect
commit 562d5247b70cb8ef8866ccd1b47c0f4590faf66a
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>

    Refactor s/filename/file-name/g; s/typename/type-name/g + fix a bug
    
    Made `phpinspect-type-name-to-file-name' more robust by adding some 
additional
    test inputs.
---
 phpinspect-autoload.el | 44 +++++++++++++++++++++++---------------------
 phpinspect-imports.el  | 19 +++++--------------
 phpinspect-project.el  | 12 ++++++------
 phpinspect-type.el     |  8 ++++----
 test/test-autoload.el  | 14 +++++++++-----
 5 files changed, 47 insertions(+), 50 deletions(-)

diff --git a/phpinspect-autoload.el b/phpinspect-autoload.el
index 866db90838..d43837fe00 100644
--- a/phpinspect-autoload.el
+++ b/phpinspect-autoload.el
@@ -89,11 +89,11 @@ execution of this strategy."))
           :type string
           :documentation "The namespace prefix for which the directories 
contain code."))
 
-(cl-defmethod phpinspect-psrX-filename-to-typename ((psr4 phpinspect-psr4) 
directory filename)
-  (phpinspect-filename-to-typename directory filename (phpinspect-psr4-prefix 
psr4)))
+(cl-defmethod phpinspect-psrX-file-name-to-type-name ((psr4 phpinspect-psr4) 
directory file-name)
+  (phpinspect-file-name-to-type-name directory file-name 
(phpinspect-psr4-prefix psr4)))
 
-(cl-defmethod phpinspect-psrX-filename-to-typename ((_psr0 phpinspect-psr0) 
directory filename)
-  (phpinspect-filename-to-typename directory filename))
+(cl-defmethod phpinspect-psrX-file-name-to-type-name ((_psr0 phpinspect-psr0) 
directory file-name)
+  (phpinspect-file-name-to-type-name directory file-name))
 
 (cl-defstruct (phpinspect-files (:constructor phpinspect-make-files))
   (autoloader nil)
@@ -225,19 +225,21 @@ Return value is a list containing instances of
     (goto-char 0)
     (phpinspect-json-preset (json-read))))
 
-(define-inline phpinspect-filename-to-typename (dir filename &optional prefix)
-  (inline-quote
-   (phpinspect-intern-name
-    (replace-regexp-in-string
-     "\\\\[\\]+"
-     "\\\\"
-     (concat "\\"
-             (or ,prefix "")
-             (replace-regexp-in-string
-              "/" "\\\\"
-              (string-remove-suffix
-               ".php"
-               (string-remove-prefix ,dir ,filename))))))))
+(define-inline phpinspect-file-name-to-type-name (dir file-name &optional 
prefix)
+  (inline-letevals ((dir (inline-quote (apply #'file-name-concat 
(file-name-split ,dir))))
+                    (file-name (inline-quote (apply #'file-name-concat 
(file-name-split ,file-name)))))
+    (inline-quote
+     (phpinspect-intern-name
+      (replace-regexp-in-string
+       "\\\\[\\]+"
+       "\\\\"
+       (concat "\\"
+               (or ,prefix "")
+               (replace-regexp-in-string
+                "/" "\\\\"
+                (string-remove-suffix
+                 ".php"
+                 (string-remove-prefix ,dir ,file-name)))))))))
 
 (defun phpinspect-find-composer-json-files (fs project-root)
   "Find all composer.json files that are relevant for a project.
@@ -303,7 +305,7 @@ re-executes the strategy."
          types-found type-fqn)
     (dolist (dir (phpinspect-psrX-directories strat))
       (dolist (file (phpinspect-fs-directory-files-recursively fs dir 
"\\.php$"))
-        (setq type-fqn (phpinspect-psrX-filename-to-typename strat dir file))
+        (setq type-fqn (phpinspect-psrX-file-name-to-type-name strat dir file))
 
         (push type-fqn types-found)
         (phpinspect-autoloader-put-type-bag al type-fqn)
@@ -462,7 +464,7 @@ FILE-NAME does not contain any wildcards, instead of nil."
 
 
 (cl-defmethod phpinspect-autoloader-resolve ((autoloader phpinspect-autoloader)
-                                             (typename (head phpinspect-name)))
+                                             (type-name (head 
phpinspect-name)))
   ;; Wait for pending refresh if not running in main thread.
   (unless (eq main-thread (current-thread))
     (when (and (phpinspect-autoloader-refresh-thread autoloader)
@@ -474,8 +476,8 @@ FILE-NAME does not contain any wildcards, instead of nil."
       (phpinspect--log "Autoload refresh completed, continuing waiting thread 
%s"
                        (thread-name (current-thread)))))
 
-  (or (gethash typename (phpinspect-autoloader-own-types autoloader))
-      (gethash typename (phpinspect-autoloader-types autoloader))))
+  (or (gethash type-name (phpinspect-autoloader-own-types autoloader))
+      (gethash type-name (phpinspect-autoloader-types autoloader))))
 
 (cl-defmethod phpinspect-autoloader-refresh ((autoloader 
phpinspect-autoloader) &optional async-callback report-progress)
   "Refresh autoload definitions by reading composer.json files
diff --git a/phpinspect-imports.el b/phpinspect-imports.el
index 0d4088ce9b..e3de5f0ab8 100644
--- a/phpinspect-imports.el
+++ b/phpinspect-imports.el
@@ -106,15 +106,15 @@ buffer position to insert the use statement at."
              (phpinspect-meta-start first-token)
              (format "%c%cuse %s;%c%c" ?\n ?\n fqn ?\n ?\n))))))))
 
-(defun phpinspect-add-use-interactive (typename buffer project &optional 
namespace-token)
+(defun phpinspect-add-use-interactive (type-name buffer project &optional 
namespace-token)
   (let* ((autoloader (phpinspect-project-autoload project))
-         (fqns (phpinspect-autoloader-get-type-bag autoloader typename)))
+         (fqns (phpinspect-autoloader-get-type-bag autoloader type-name)))
     (cond ((= 1 (length fqns))
            (phpinspect-add-use (phpinspect-name-string (car fqns)) buffer 
namespace-token))
           ((> (length fqns) 1)
            (phpinspect-add-use (completing-read "Class: " 
(phpinspect-names-to-alist fqns))
                                buffer namespace-token))
-          (t (phpinspect-message "No import found for type %s" 
(phpinspect-name-string typename))))))
+          (t (phpinspect-message "No import found for type %s" 
(phpinspect-name-string type-name))))))
 
 (defalias 'phpinspect-fix-uses-interactive #'phpinspect-fix-imports
   "Alias for backwards compatibility")
@@ -235,7 +235,7 @@ determine the scope of the imports (global or local 
namespace)."
                              nil namespace-name (phpinspect-name-string type)))
                            (phpinspect-autoloader-types
                             (phpinspect-project-autoload project))))
-        (unless (member (phpinspect-name-string type) 
phpinspect-native-typenames)
+        (unless (member (phpinspect-name-string type) 
phpinspect-native-type-names)
           (phpinspect-add-use-interactive type buffer project namespace)
           (phpinspect-buffer-parse buffer 'no-interrupt))))))
 
@@ -321,16 +321,7 @@ that there are import (\"use\") statements for them."
 
   (if phpinspect-current-buffer
       (let* ((buffer phpinspect-current-buffer)
-             ;; use buffer-reparse to ensure fully structurally correct
-             ;; tree. (at the time of writing, incremental parsing has some
-             ;; limitations causing reused tokens to lose their special meaning
-             ;; when they are reused. For example the "class" keyword being
-             ;; reused as just a word instead of a special keyword marking the
-             ;; start of a class)
-             ;;
-             ;; FIXME: Change to buffer-parse when this particular problem in
-             ;; incremental parsing has been solved
-             (tree (phpinspect-buffer-reparse-if-not-fresh buffer))
+             (tree (phpinspect-buffer-parse buffer 'no-interrupt))
              (index (phpinspect--index-tokens
                      tree nil (phpinspect-buffer-location-resolver buffer)))
              (namespaces (alist-get 'namespaces index))
diff --git a/phpinspect-project.el b/phpinspect-project.el
index d0f663316a..e6f99391ff 100644
--- a/phpinspect-project.el
+++ b/phpinspect-project.el
@@ -336,15 +336,15 @@ before the search is executed."
      nil)))
 
 (cl-defmethod phpinspect-project-index-file
-  ((project phpinspect-project) (filename string))
+  ((project phpinspect-project) (file-name string))
   "Index "
   (let ((fs (phpinspect-project-fs project)))
     (with-temp-buffer
-      (phpinspect-fs-insert-file-contents fs filename 'prefer-async)
+      (phpinspect-fs-insert-file-contents fs file-name 'prefer-async)
       (phpinspect-index-current-buffer))))
 
-(cl-defmethod phpinspect-project-add-file-index ((project phpinspect-project) 
(filename string))
-  (phpinspect-project-add-index project (phpinspect-project-index-file project 
filename)))
+(cl-defmethod phpinspect-project-add-file-index ((project phpinspect-project) 
(file-name string))
+  (phpinspect-project-add-index project (phpinspect-project-index-file project 
file-name)))
 
 (defcustom phpinspect-projects nil
   "PHPInspect Projects."
@@ -354,8 +354,8 @@ before the search is executed."
   :group 'phpinspect)
 
 (defun phpinspect-project-make-file-indexer (project)
-  (lambda (filename)
-    (phpinspect-project-add-file-index project filename)))
+  (lambda (file-name)
+    (phpinspect-project-add-file-index project file-name)))
 
 (defun phpinspect-project-make-root-resolver (project)
   (lambda () (phpinspect-project-root project)))
diff --git a/phpinspect-type.el b/phpinspect-type.el
index e05778b5b3..91bfa807bb 100644
--- a/phpinspect-type.el
+++ b/phpinspect-type.el
@@ -65,7 +65,7 @@ that the collection is expected to contain")
   (mapcar (lambda (name) (phpinspect--make-type :name name))
           type-name-strings))
 
-(defconst phpinspect-native-typenames
+(defconst phpinspect-native-type-names
   ;; self, parent and resource are not valid type name.
   ;; see https://www.php.net/manual/ja/language.types.declarations.php
   ;;;
@@ -75,7 +75,7 @@ that the collection is expected to contain")
 
 (defvar phpinspect-native-types
   (phpinspect--make-types (mapcar (lambda (name) (concat "\\" name))
-                                  phpinspect-native-typenames)))
+                                  phpinspect-native-type-names)))
 
 (defvar phpinspect-collection-types
   (phpinspect--make-types '("\\array" "\\iterable" "\\SplObjectCollection" 
"\\mixed"))
@@ -91,7 +91,7 @@ that the collection is expected to contain")
 (defun phpinspect-define-standard-types ()
   (setq phpinspect-native-types
         (phpinspect--make-types (mapcar (lambda (name) (concat "\\" name))
-                                        phpinspect-native-typenames))
+                                        phpinspect-native-type-names))
         phpinspect-collection-types (phpinspect--make-types
                                      '("\\array" "\\iterable" 
"\\SplObjectCollection" "\\mixed"))
         phpinspect--object-type (phpinspect--make-type :name "\\object" 
:fully-qualified t)
@@ -174,7 +174,7 @@ NAMESPACE may be nil, or a string with a namespace FQN."
          type)
 
         ;; Native type
-        ((member type phpinspect-native-typenames)
+        ((member type phpinspect-native-type-names)
          (concat "\\" type))
 
         ;; Relative FQN
diff --git a/test/test-autoload.el b/test/test-autoload.el
index 241ed5e921..e210c041dc 100644
--- a/test/test-autoload.el
+++ b/test/test-autoload.el
@@ -33,11 +33,6 @@
                            (file-name-directory (macroexp-file-name))))
 
 
-(ert-deftest phpinspect-filename-to-typename ()
-  (should (eq (phpinspect-intern-name "\\Foo\\Bar") 
(phpinspect-filename-to-typename "src/" "src/Foo////////Bar.php")))
-
-  (should (eq (phpinspect-intern-name "\\Foo\\Bar") 
(phpinspect-filename-to-typename "src/somewhere/else/" 
"src/somewhere/else/Foo/Bar.php"))))
-
 
 (ert-deftest phpinspect-find-composer-json-files ()
   (let* ((fs (phpinspect-make-virtual-fs)))
@@ -215,3 +210,12 @@
 
     ;; If file was already known to autoloader, do nothing
     (should-not (phpinspect-autoloader-ensure-file-indexed autoloader 
"/project/root/src/Peanuts.php"))))
+
+(ert-deftest phpinspect-file-name-to-type-name ()
+  (let ((names `((("/var/////src///app" "/var/src/app//Controller.php" 
"\\App") "\\App\\Controller")
+                 (("src/" "src/Foo////////Bar.php") "\\Foo\\Bar")
+                 (("src/" "src////Foo////////Bar.php") "\\Foo\\Bar")
+                 (("src/somewhere/else/" "src/somewhere/else/Foo/Bar.php") 
"\\Foo\\Bar"))))
+
+    (dolist (name names)
+      (should (eq (phpinspect-intern-name (cadr name)) (apply 
#'phpinspect-file-name-to-type-name (car name)))))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]