 
 (defun php-helpers/sort-uses-after-save-handler ()
   "Handler for `after-save-hook' to automatically sort use-statements, iff
-  `php-helpers-sort-uses-on-save' is t."
+`php-helpers-sort-uses-on-save' is t."
   (when php-helpers-sort-uses-on-save
     (php-helpers/sort-uses)))
 

                          name (file-name-nondirectory new-name)))))
      (error "Buffer is not visiting a file"))))

(provide 'php-helpers)

;;; php-helpers.el ends here

      (set-window-point nil return-point)
      (evil-insert-state))))

(defvar php-helpers--class-name-regex (rx
                                       line-start
                                       (group (zero-or-one "final"
                                                           (one-or-more " "))
                                              (or "class" "trait" "interface")
                                              (one-or-more " "))
                                       (group (one-or-more (syntax word))))
  "Regex for extracting the class/trait/interface's name.")

(cl-defun php-helpers//buffer-class-name (&optional (buffer (current-buffer)))
  "Return the name of a class/trait/interface defined in BUFFER.

If there are multiple such definitions, the first one is returned."
  (with-current-buffer buffer
    (save-excursion
      (save-restriction
        (evil-with-state 'emacs-state
          (widen)
          (beginning-of-buffer)
          (when (re-search-forward php-helpers--class-name-regex nil t)
            (match-string-no-properties 2)))))))

;;;###autoload
(cl-defun php-helpers/rename-class (new-name
                                    &optional (buffer (current-buffer)))
  "Replace the class name in the topmost class/trait/interface declaration."
  (interactive "MNew class name: ")
  (with-current-buffer buffer
    (save-excursion
      (save-restriction
        (evil-with-state 'emacs-state
          (widen)
          (beginning-of-buffer)
          (when (re-search-forward php-helpers--class-name-regex nil t)
            (replace-match (concat "\\1" new-name))))))))

;;;###autoload
(defun php-helpers/rename-file-to-class-name ()
  "Rename the current buffer's visited file according to its PHP class name.
This operation is inverse of `php-helpers/rename-class-to-file-name'."
  (interactive)
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (if (and filename (file-exists-p filename))
        ;; the buffer is visiting a file
        (let* ((dir (file-name-directory filename))
               (new-name (concat dir (php-helpers//buffer-class-name)
                                 ".php")))
          (cond ((get-buffer new-name)
                 (error "A buffer named '%s' already exists!" new-name))
