When authoring a class it is redundant effort to add a property and
documentation tag when making an assignment. This refactoring will scan
for any assignments which have do not have corresponding properties and
add the required properties with docblocks based on inferred types where
possible.
Phpactor already has the possibility of generating new classes, and
moving classes. But sometimes your project may get into a state where
class-containing files have an incorrect namespace or class name.
This refactoring will:
Update the namespace based on the file path (and the autoloading
config).
Update the class name.
When given an empty file, it will generate a PHP tag and the
namespace.
It can be very tiresome to manually implement contracts for interfaces
and abstract classes, especially interfaces with many methods
(e.g. ArrayAccess).
This refactoring will automatically add the required methods to your
class. If the interface uses any foreign classes, the necessary use
statements will also be added.
It is easy to remember the name of a class, but more difficult to
remember its namespace, and certainly it is time consuming to manually
code class imports:
Manually one would:
Perform a fuzzy search for the class by its short name.
Although importing classes can make code cleaner, sometimes the code can
be more readable if the fully qualified name is specified. For example,
we might register a list of listeners in a file.
You may copy and paste some code from one file to another and
subsequently need to import all the foreign classes into the current
namespace. This refactoring will identify all unresolvable classes and
import them.
This example is from an existing, empty, file. Note that you can also
use the context menu to generate classes from non-existing class
names in the current file
Given a new file:
# src/Blog/Post.php
After invoking class new using the default variant:
It is sometimes unwise to preemptively create interfaces for all your
classes, as doing so adds maintenance overhead, and the interfaces may
never be needed.
This refactoring allows you to generate an interface from an existing
class. All public methods will be added to generated interface.
When authoring classes, it is often difficult to determine really
appropriate names and namespaces, this is unfortunate as a class name
can quickly propagate through your code, making the class name harder to
change as time goes on.
This problem is multiplied if you have chosen an incorrect namespace.
This refactoring will move either a class, class-containing-file or
folder to a new location, updating the classes namespace and all
references to that class where possible in a given scope (i.e. files
known by GIT: git, files known by Composer: composer, or all PHP
files under the current CWD: simple).
If you have defined file relationships with
navigator.destinations,
then you have the option to move the related files in addition to the
specified file. If using the command then specify --related, or if
using the RPC interface (f.e. VIM) you will be prompted.
This is a dangerous refactoring! Ensure that you commit your work
before executing it and be aware that success is not guaranteed
(e.g. class references in non-PHP files or docblocks are not
currently updated).
This refactoring works best when you have a well tested code base.
Each time a value is duplicated in a class a fairy dies. Duplicated
values increase the fragility of your code. Replacing them with a
constant helps to ensure runtime integrity.
Having meaningful and descriptive variable names makes the intention of
code clearer and therefore easier to maintain. Renaming variables is a
frequent refactoring, but doing this with a simple search and replace
can often have unintended consequences (e.g. renaming the variable
$class also changes the class keyword).
This refactoring will rename a variable, and only variables, in either
the method scope or the class scope.
This refactoring is similar to Move Class, but
without renaming the file. This is a useful refactoring when a dependant
library has changed a class name and you need to update that class name
in your project.
Having an API which is expressive of the intent of the class is
important, and contributes to making your code more consistent and
maintainable.
When renaming members global search and replace can be used, but is a
shotgun approach and you may end up replacing many things you did not
mean to replace (e.g. imagine renaming the method name()).
This refactoring will:
Scan for files in your project which contain the member name text.
Parse all of the candidate files.
Identify the members, and try and identify the containing class.
Replace only the members which certainly belong to the target class.
When replacing private and protected members, only the related
classes will be updated.
Due to the loosely typed nature of PHP this refactoring may not find all
of the member accesses for the given class. Run your tests before and
after applying this refactoring.
Hint: Use the CLI command to list all of the risky references. Risky
references are those member accesses which match the query but whose
containing classes could not be resolved.