Skip to content

Commit

Permalink
Automatically remove autoloads leading to non-existing dirs
Browse files Browse the repository at this point in the history
Do not create autoloaders leading to non-existing dirs. This prevents
bugs when trying to reach these namespaces.
  • Loading branch information
stevenremot committed Apr 6, 2017
1 parent 28a9892 commit 39c0ea9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Next

Features:
- Automatically remove autoloads leading to non-existing dir

Fixes:

# 1.1.0

Features:
Expand Down
62 changes: 55 additions & 7 deletions ede-php-autoload.el
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,52 @@ to the associated directories."
(add-to-list 'loaders (ede-php-autoload-class-loader-call-factory key (cadr load-config)))
(setq load-config (cddr load-config))))
(ede-php-autoload-aggregate-class-loader "Aggregate loader"
:class-loaders loaders)))
:class-loaders loaders)))

(defun ede-php-autoload-remove-non-existing-dirs (conf root-dir)
"Remove from CONF the non-existing directories.
CONF is the same kind of argument than `ede-php-autoload-create-class-loader'.
ROOT-DIR is the root directory of the project."
(let ((cleaned-conf '())
(conf-length (length conf))
(index 0)
key
namespaces cleaned-namespaces
namespace
paths cleaned-paths)

;; key: `:psr-0', ...
(while (< index conf-length)
(setq key (nth index conf)
namespaces (nth (1+ index) conf)
cleaned-namespaces '())

;; pair: (ns . paths)
(dolist (pair namespaces)
(setq namespace (car pair)
paths (if (listp (cdr pair)) (cdr pair) (list (cdr pair)))
cleaned-paths '())

(dolist (path paths)
(when (file-exists-p (expand-file-name path root-dir))
(add-to-list 'cleaned-paths path t)))

(when (> (length cleaned-paths) 0)
(add-to-list 'cleaned-namespaces
(cons
namespace
(if (= (length cleaned-paths) 1)
(car cleaned-paths)
cleaned-paths))
t)))

(when (> (length cleaned-namespaces) 0)
(setq cleaned-conf (append cleaned-conf (list key cleaned-namespaces))))

(setq index (+ index 2)))
cleaned-conf))


(defclass ede-php-autoload-target (ede-target)
Expand Down Expand Up @@ -205,12 +250,15 @@ to the associated directories."
This can be used when some composer dependencies changed, to take
the new autoloads into account."
(oset this class-loader
(ede-php-autoload-create-class-loader
(ede-php-autoload--append-composer-autoload-data
(file-name-directory (ede-project-root-directory this))
(oref this explicit-class-autoloads))
)))
(let* ((raw-autoloads
(ede-php-autoload--append-composer-autoload-data
(file-name-directory (ede-project-root-directory this))
(oref this explicit-class-autoloads)))
(root-dir (ede-project-root-directory this))
(cleaned-autoloads (ede-php-autoload-remove-non-existing-dirs raw-autoloads root-dir)))

(oset this class-loader
(ede-php-autoload-create-class-loader cleaned-autoloads))))

(defmethod ede-find-subproject-for-directory ((proj ede-php-autoload-project) dir)
"Return PROJ, for handling all subdirs below DIR."
Expand Down
4 changes: 4 additions & 0 deletions features/ede-php-autoload-completion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ Feature: Class name completion
Given I visit "src/main.php" in project "without-composer"
Then completions for query "ClassMapNs\" should be:
| ClassMapNs\MyClass |
Scenario: Complete non-existing dir
Given I visit "src/main.php" in project "without-composer"
Then completions for query "NonExisting\" should be nil
2 changes: 1 addition & 1 deletion features/ede-php-autoload.feature
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ Feature: Hand-made EDE project creation
THen guessing the class name for "src/ClassMapNs/MyClass.php" should return "ClassMapNs\MyClass"


Scenario: Load a class the doesn't exist
Scenario: Load a class that doesn't exist
Given I visit "src/main.php" in project "without-composer"
Then the class "Psr4Ns\DoesNotExist" should not be detected
4 changes: 4 additions & 0 deletions features/step-definitions/ede-php-autoload-steps.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@
(lambda (query suggestion-table)
(should (equal (ede-php-autoload-complete-type-name (ede-current-project) query)
(car suggestion-table)))))

(Then "^completions for query \"\\(.+\\)\" should be nil"
(lambda (query)
(should (null (ede-php-autoload-complete-type-name (ede-current-project) query)))))
3 changes: 2 additions & 1 deletion features/support/env.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ COMPOSER-FILE-NAME is either default or new."
:psr-4 (("Psr4Ns" . "src/Psr4Ns")
("MultiDirNs" . ("src/MultiDirNs1" "src/MultiDirNs2"))
("Psr4Split\\Ns1" . "src/Psr4Split/Ns1")
("Psr4Split\\Ns2" . "src/Psr4Split/Ns2"))
("Psr4Split\\Ns2" . "src/Psr4Split/Ns2")
("NonExisting" . "src/NonExisting"))
:class-map ((ClassMapNs\\MyClass . "src/ClassMapNs/MyClass.php")))
:include-path '(".")
:system-include-path '("/usr/share/php")))

0 comments on commit 39c0ea9

Please sign in to comment.