0

I am a Vim user for about a year now and I am curious to give Emacs a try. I discover Emacs for some days now and I would like to improve my PHP-editing experience.
In Vim there is a plugin for phpactor, a smart autocompletion tool for PHP, as it's done in IDE.
There is also an Emacs plugin (in alpha version as of writing), and I can't get it to work.

To install Phpactor I followed the documentation, so I have a global phpactor command that I can use.
As an example here is the output when I run it in my Terminal:

$ phpactor status
Version: 7db7a5c (3 weeks ago) updated-composer-749
Filesystems: git, simple, composer
Working directory: /var/www/html/work/libs/myrepo

Config files (missing is not bad):

  ✘ /home/john/.config/phpactor/phpactor.json
  ✘ /home/john/config/phpactor/phpactor.yml
  ✘ /var/www/html/work/libs/myrepo/.phpactor.json
  ✘ /var/www/html/work/libs/myrepo/.phpactor.yml

Diagnostics:

  ✔ Composer detected - faster class location and more features!
  ✔ Git detected - enables faster refactorings in your repository scope!
  ✔ XDebug is disabled. XDebug has a negative effect on performance.

By viewing so, I think it's installed successfully.
In Emacs I installed the plugin by using the command package-install phpactor.
My ~/.emacs contains the following as suggested by the plugin documentation:

(add-hook 'php-mode-hook
          (lambda ()
            (make-local-variable 'eldoc-documentation-function)
            (setq eldoc-documentation-function
                  'phpactor-hover)))

Now that everything is installed I would like to use it and I don't know how to. I think phpactor is called when I am about to call a method on a variable, but I only see an error in the mini buffer. Here is an example:

function thing() {
    $a = new Stuff();
    $a->|
    return 1;
}

When my cursor is just after the arrow (where I put a pipe |), I see the following in the mini buffer:

Phpactor: Unknown item "return", known items: "return", "return", "return"

In Vim I only need to install the plugin and type C-x C-o to have a context menu with the suggested method names.
How to use it in Emacs?

  • Ensure that you have phpactor.el installed according to its directions. Barring that, understand that phpactor.el is alpha quality software. Your best bet for getting help may be to open an issue at its github page. – nega Apr 11 '19 at 17:05
  • I understand it's in alpha for now. I think I've installed it the right way, but I'll open an issue on its Github page, thank you for the advice. – AnthonyB Apr 12 '19 at 8:04
1

Here is my code. Yes the documentation is awful for phpactor and it takes some digging. I have this currently and it shows how to use transients and bind them to a key in php-mode. Hopefully useful to someone.

 (use-package transient
   :ensure t)

 (use-package phpactor 
   :ensure t)

 (use-package company-phpactor 
   :ensure t)

 (use-package php-mode
   ;;
   :hook (
          (php-mode . (lambda () 
                        (make-local-variable 'eldoc-documentation-function)
                        (setq eldoc-documentation-function
                              'phpactor-hover)
                        (set (make-local-variable 'company-backends)
                             '(;; list of backends
                               company-phpactor
                               company-files))
                        (lsp)
                        )))
   :config
   (add-to-list 'auto-mode-alist '("\\.html\\.twig\\'" . web-mode))
   (define-transient-command php-transient-menu ()
     "Php"
     [["Class"
       ("cc" "Copy" phpactor-copy-class)
       ("cn" "New" phpactor-create-new-class)
       ("cr" "Move" phpactor-move-class)
       ("ci" "Inflect" phpactor-inflect-class)
       ("n"  "Namespace" phpactor-fix-namespace)]
      ["Properties"
       ("a"  "Accessor" phpactor-generate-accessors)
       ("pc" "Constructor" phpactor-complete-constructor)
       ("pm" "Add missing props" phpactor-complete-properties)
       ("r" "Rename var locally" phpactor-rename-variable-local)
       ("R" "Rename var in file" phpactor-rename-variable-file)]
      ["Extract"
       ("ec" "constant" phpactor-extract-constant)
       ("ee" "expression" phpactor-extract-expression)
       ("em"  "method" phpactor-extract-method)]
      ["Methods"
       ("i" "Implement Contracts" phpactor-implement-contracts)
       ("m"  "Generate method" phpactor-generate-method)]
      ["Navigate"
       ("x" "List refs" phpactor-list-references)
       ("X" "Replace refs" phpactor-replace-references)
       ("."  "Goto def" phpactor-goto-definition)]
      ["Phpactor"
       ("s" "Status" phpactor-status)
       ("u" "Install" phpactor-install-or-update)]])

   (define-key php-mode-map (kbd "C-p") 'php-transient-menu))
| improve this answer | |
  • Thank you for your answer. The transient command are only key binding? How do you use them? – AnthonyB Dec 5 '19 at 14:12
0

I followed the @nega's advice and I've submitted a Github issue.

In short, the part in my ~/.emacs was problematic, I had to delete it. I mean this part:

(add-hook 'php-mode-hook
          (lambda ()
            (make-local-variable 'eldoc-documentation-function)
            (setq eldoc-documentation-function
                  'phpactor-hover)))

Also there is no keybindings by default and I didn't know it.

EDIT:

Finally I used the bind-key plugin to map keys and phpactor functions, like this :

(require 'bind-key)
(bind-key* "C-p C-d" 'phpactor-goto-definition)
(bind-key* "C-p C-i" 'phpactor-implement-contracts)
(bind-key* "C-p C-g" 'phpactor-generate-method)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.