[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phpinspect 153ff71fcf 048/126: WIP: Implement psr0 and
From: |
ELPA Syncer |
Subject: |
[elpa] externals/phpinspect 153ff71fcf 048/126: WIP: Implement psr0 and psr4 autoload strategies |
Date: |
Sat, 12 Aug 2023 00:58:40 -0400 (EDT) |
branch: externals/phpinspect
commit 153ff71fcf4c035f336be0c759a110aba411ef0a
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>
WIP: Implement psr0 and psr4 autoload strategies
---
README.md | 2 +-
phpinspect-autoload.el | 152 ++++++++++++++++++++++++++++++++++++++++++++++++
test/phpinspect-test.el | 1 +
test/test-autoload.el | 112 +++++++++++++++++++++++++++++++++++
4 files changed, 266 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index fb0011dc3f..cb50b05541 100644
--- a/README.md
+++ b/README.md
@@ -45,5 +45,5 @@ Tests are implemented using `ert`. You can run them in batch
mode with the follo
command:
```bash
-emacs -batch -l ert -l ./phpinspect.el -l ./test/phpinspect-test.el -f
ert-run-tests-batch-and-exit
+emacs -L ./ -batch -l ert -l ./phpinspect.el -l ./test/phpinspect-test.el -f
ert-run-tests-batch-and-exit
```
diff --git a/phpinspect-autoload.el b/phpinspect-autoload.el
new file mode 100644
index 0000000000..c5721cdc71
--- /dev/null
+++ b/phpinspect-autoload.el
@@ -0,0 +1,152 @@
+;;; phpinspect-autoload.el --- PHP parsing and completion package -*-
lexical-binding: t; -*-
+
+;; Copyright (C) 2021 Free Software Foundation, Inc
+
+;; Author: Hugo Thunnissen <devel@hugot.nl>
+;; Keywords: php, languages, tools, convenience
+;; Version: 0
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'cl-lib)
+
+(cl-defstruct (phpinspect-autoloader
+ (:constructor phpinspect-make-autoloader))
+ (types (make-hash-table :test 'eq)
+ :type hash-table
+ :documentation
+ "The types that can be autoloaded through this autoloader.")
+ (strategies nil
+ :type list
+ :documentation
+ "The strategies that this autoloader can employ for its
purpose."))
+
+(cl-defstruct (phpinspect-directory
+ (:constructor phpinspect-make-directory))
+ (location nil
+ :type string
+ :documentation
+ "The filepath to the directory"))
+
+(cl-defstruct (phpinspect-virtual-directory
+ (:constructor phpinspect-make-virtual-directory))
+ (location nil
+ :type string
+ :documentation
+ "The filepath to the directory")
+ (files (make-hash-table :test 'equal)
+ :type hash-table
+ :documentation
+ "The files in the virtual directory"))
+
+(cl-defgeneric phpinspect-directory-list-files (directory)
+ "List all PHP files in DIRECTORY")
+
+(cl-defmethod phpinspect-directory-list-files ((dir phpinspect-directory))
+ "List all PHP files in DIR."
+ (directory-files-recursively (phpinspect-directory-location dir)
+ ".*.php$"
+ t ;; Ignore directories that cannot be read
+ t ;; follow symlinks
+ ))
+
+(cl-defmethod phpinspect-directory-list-files ((dir
phpinspect-virtual-directory))
+ "List all virtual files that DIR contains."
+ (let ((files))
+ (maphash (lambda (file _)
+ (push file files))
+ (phpinspect-virtual-directory-files dir))
+ files))
+
+(cl-defgeneric phpinspect-directory-list-files (directory)
+ "List all PHP files in DIRECTORY")
+
+(cl-defmethod phpinspect-directory-get-location ((dir phpinspect-directory))
+ "Get location of PHP dir."
+ (phpinspect-directory-location dir))
+
+(cl-defmethod phpinspect-directory-get-location ((dir
phpinspect-virtual-directory))
+ "List all virtual files that DIR contains."
+ (phpinspect-virtual-directory-location dir))
+
+(cl-defmethod phpinspect-directory-insert-file-contents ((dir
phpinspect-directory)
+ (file string))
+ (insert-file-contents file))
+
+(cl-defmethod phpinspect-directory-insert-file-contents ((dir
phpinspect-virtual-directory)
+ (file string))
+ (insert (gethash file (phpinspect-virtual-directory-files dir))))
+
+(cl-defstruct (phpinspect-psr0
+ (:constructor phpinspect-make-psr0-generated))
+ (prefix nil
+ :type string
+ :documentation "The namespace prefix for which the directories
contain code.")
+ (directories nil
+ :type list
+ :documentation
+ "The directories that this autoloader finds code in."))
+
+(cl-defstruct (phpinspect-psr4
+ (:constructor phpinspect-make-psr4-generated))
+ (prefix nil
+ :type string
+ :documentation "The namespace prefix for which the directories
contain code.")
+ (directories nil
+ :type list
+ :documentation
+ "The directories that this autoloader finds code in."))
+
+(cl-defstruct (phpinspect-classmap
+ (:constructor phpinspect-make-classmap-generated))
+ (directories nil
+ :type list
+ :documentation
+ "The directories that this autoloader finds code in."))
+
+(cl-defgeneric phpinspect-al-strategy-fill-typehash (strategy typehash)
+ "Make STRATEGY return a map with type names as keys and the
+ paths to the files they are defined in as values.")
+
+(defsubst phpinspect-filename-to-typename (dir filename &optional prefix)
+ (phpinspect-intern-name
+ (concat "\\"
+ (or prefix "")
+ (replace-regexp-in-string "/"
+ "\\\\"
+ (string-remove-suffix
+ ".php"
+ (string-remove-prefix
+ (phpinspect-directory-get-location dir)
+ filename))))))
+
+(cl-defmethod phpinspect-al-strategy-fill-typehash ((strategy phpinspect-psr0)
+ typehash)
+ (dolist (dir (phpinspect-psr0-directories strategy))
+ (dolist (file (phpinspect-directory-list-files dir))
+ (puthash (phpinspect-filename-to-typename dir file) file typehash))))
+
+(cl-defmethod phpinspect-al-strategy-fill-typehash ((strategy phpinspect-psr4)
+ typehash)
+ (let ((prefix (phpinspect-psr4-prefix strategy)))
+ (dolist (dir (phpinspect-psr4-directories strategy))
+ (dolist (file (phpinspect-directory-list-files dir))
+ (puthash (phpinspect-filename-to-typename dir file prefix) file
typehash)))))
+
+(provide 'phpinspect-autoload)
+;;; phpinspect-autoload.el ends here
diff --git a/test/phpinspect-test.el b/test/phpinspect-test.el
index 0774bb2116..e9fb7efbca 100644
--- a/test/phpinspect-test.el
+++ b/test/phpinspect-test.el
@@ -498,6 +498,7 @@ class Thing
(phpinspect-parse-string php-code-bare))))))
(load-file (concat phpinspect-test-directory "/test-worker.el"))
+(load-file (concat phpinspect-test-directory "/test-autoload.el"))
(provide 'phpinspect-test)
;;; phpinspect-test.el ends here
diff --git a/test/test-autoload.el b/test/test-autoload.el
new file mode 100644
index 0000000000..e38d41d1f8
--- /dev/null
+++ b/test/test-autoload.el
@@ -0,0 +1,112 @@
+;;; test-autoload.el --- Unit tests for phpinspect.el -*- lexical-binding: t;
-*-
+
+;; Copyright (C) 2021 Free Software Foundation, Inc.
+
+;; Author: Hugo Thunnissen <devel@hugot.nl>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+
+(require 'ert)
+(require 'phpinspect-autoload)
+
+(ert-deftest phpinspect-psr0-fill-typehash ()
+ (let* ((directory1 (phpinspect-make-virtual-directory
+ :location "/home/user/projects/app/src/"))
+ (directory2 (phpinspect-make-virtual-directory
+ :location "/home/user/projects/app/lib/"))
+ (typehash (make-hash-table :size 10 :test 'eq))
+ (autoload
+ (phpinspect-make-psr0-generated :prefix "App\\")))
+
+ (puthash "/home/user/projects/app/src/App/Services/SuperService.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+
+ (puthash "/home/user/projects/app/src/Kernel.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+ (puthash "/home/user/projects/app/src/App/Controller/Banana.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+
+ (puthash "/home/user/projects/app/lib/Mailer_Lib.php"
+ ""
+ (phpinspect-virtual-directory-files directory2))
+
+ (setf (phpinspect-psr0-directories autoload) (list directory1 directory2))
+
+ (phpinspect-al-strategy-fill-typehash autoload typehash)
+
+ (should (string=
"/home/user/projects/app/src/App/Services/SuperService.php"
+ (gethash (phpinspect-intern-name
"\\App\\Services\\SuperService")
+ typehash)))
+ (should (string= "/home/user/projects/app/src/Kernel.php"
+ (gethash (phpinspect-intern-name "\\Kernel")
+ typehash)))
+ (should (string= "/home/user/projects/app/src/App/Controller/Banana.php"
+ (gethash (phpinspect-intern-name
"\\App\\Controller\\Banana")
+ typehash)))
+
+ (should (string= "/home/user/projects/app/lib/Mailer_Lib.php"
+ (gethash (phpinspect-intern-name "\\Mailer_Lib")
+ typehash)))))
+
+(ert-deftest phpinspect-psr4-fill-typehash ()
+ (let* ((directory1 (phpinspect-make-virtual-directory
+ :location "/home/user/projects/app/src/"))
+ (directory2 (phpinspect-make-virtual-directory
+ :location "/home/user/projects/app/lib/"))
+ (typehash (make-hash-table :size 10 :test 'eq))
+ (autoload
+ (phpinspect-make-psr4-generated :prefix "App\\")))
+
+ (puthash "/home/user/projects/app/src/Services/SuperService.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+
+ (puthash "/home/user/projects/app/src/Kernel.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+ (puthash "/home/user/projects/app/src/Controller/Banana.php"
+ ""
+ (phpinspect-virtual-directory-files directory1))
+
+ (puthash "/home/user/projects/app/lib/Mailer_Lib.php"
+ ""
+ (phpinspect-virtual-directory-files directory2))
+
+ (setf (phpinspect-psr4-directories autoload) (list directory1 directory2))
+
+ (phpinspect-al-strategy-fill-typehash autoload typehash)
+
+ (should (string= "/home/user/projects/app/src/Services/SuperService.php"
+ (gethash (phpinspect-intern-name
"\\App\\Services\\SuperService")
+ typehash)))
+ (should (string= "/home/user/projects/app/src/Kernel.php"
+ (gethash (phpinspect-intern-name "\\App\\Kernel")
+ typehash)))
+ (should (string= "/home/user/projects/app/src/Controller/Banana.php"
+ (gethash (phpinspect-intern-name
"\\App\\Controller\\Banana")
+ typehash)))
+
+ (should (string= "/home/user/projects/app/lib/Mailer_Lib.php"
+ (gethash (phpinspect-intern-name "\\App\\Mailer_Lib")
+ typehash)))))
- [elpa] externals/phpinspect 2b85271b2b 030/126: Test + fix get-last-statement-in-token for static attribute references, (continued)
- [elpa] externals/phpinspect 2b85271b2b 030/126: Test + fix get-last-statement-in-token for static attribute references, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect a60dba5f11 031/126: WIP: new struct to represent types, and string comparison optimization with obarray, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 8caf967b57 029/126: Add test for resolve-type-from-context with preceding bareword, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 341afd42da 037/126: WIP: Index types in the background using collaborative threading., ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 98e88d45ac 034/126: WIP: Fix find-class-file, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 350850c07a 052/126: Fix phpinspect-index-static-methods test, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 224bbd7916 057/126: Implement array member type inference, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 9882ed2c60 061/126: Test/fix type inference of objects in nested arrays, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 47335f3450 064/126: Refactor phpinspect--project to phpinspect-project, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 1b1cf45638 054/126: Fix bug in extended classes' method merging + add some tests, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 153ff71fcf 048/126: WIP: Implement psr0 and psr4 autoload strategies,
ELPA Syncer <=
- [elpa] externals/phpinspect e65b268cea 053/126: Implement @method annotation indexation, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect ae3acbdbe1 056/126: Disable auto-reindexing by default, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect e7b1f22e8c 068/126: Sort tokens by size when returning tokens around point, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 0e00d7e5a6 066/126: Wakeup worker when stop is requested and worker thread is paused, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect bb04e9a0f8 079/126: Implement strategy pattern for phpinspect-eldoc-function, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect f9f3440850 065/126: Make file contents insertion asynchronous in background threads, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect ad5ede01ad 071/126: Implement Incremental Parsing, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 9d6ce5726d 076/126: Use `phpinspect-edtrack-original-position-at-point' for edit end determination, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 58ad65932b 069/126: Add `phpinspect-parser' type and `phpinspect-defparser' macro, ELPA Syncer, 2023/08/12
- [elpa] externals/phpinspect 5548734ef7 075/126: Implement parser interruption on user input, ELPA Syncer, 2023/08/12