Skip to content

nvs code

nvs code prints information about the code of the project you are in, and it can change that code for you.

refs, calls, impls and types show where a name is used, which methods call a method, and which type a variable has. unused lists each declaration that nothing uses, and delete deletes one.

rename renames a name everywhere. nvs code rename 'Shop\Cart' 'Shop\Billing\Cart' moves a class to another namespace. rename --from <file> applies a list of renames in one change. move moves a file or a method. extract writes a new variable, method or interface. inline replaces a variable or a call with its value. signature changes a method's parameters at every call. implement and order write missing methods and sort class members.

Each command that changes files takes --dry-run and --json. If the new code would not compile, or a file changes while the command runs, the command changes nothing and prints why. When project files have errors, every command prints a warning with their names.

Good to know: write the name with its namespace, in single quotes. Without the quotes, the shell removes the backslashes and the name matches nothing.

Examples

Who calls a method

<?nvs
// Shows the `nvs code` commands that find who calls a method and which classes implement an interface.
namespace Shop;
interface Priced
{
public function price(): int;
}
class Book implements Priced
{
public function price(): int
{
return 12;
}
}
class Lamp implements Priced
{
public function price(): int
{
return 30;
}
}
// `nvs code refs 'Shop\Priced::price'` prints the line where `Priced` declares `price`.
// Then it prints the call in `total`, with its file, line and column.
class Basket
{
public static function total(array<Priced> $items): int
{
int $total = 0;
foreach ($items as Priced $item) {
$total += $item->price();
}
return $total;
}
// `nvs code calls 'Shop\Basket::total' 'Shop\Basket::withShipping'` prints a line with each name.
// Under it are the methods that call it and the methods it calls.
public static function withShipping(array<Priced> $items): int
{
return Basket::total($items) + 5;
}
}
// `nvs code impls 'Shop\Priced'` prints one line for `Shop\Book` and one for `Shop\Lamp`.
// Each line starts with the file, the line and the column of the class.
array<Priced> $basket = [new Book(), new Lamp(), new Book()];
// The basket costs 12 + 30 + 12, plus 5 for shipping. This prints `59`.
echo Basket::withShipping($basket), "\n";
Output
59

Order class members

<?nvs
// Shows a class after `nvs code order` put its members in the standard order.
namespace Shop;
// The command put the constant first, then the property, then the methods.
// The static method is before the constructor, and the private method is last.
class Cart
{
public const int MAX = 10;
private int $count = 0;
public static function empty(): Cart
{
return new Cart(0);
}
public function constructor(int $count)
{
$this->count = $this->limited($count);
}
public function count(): int
{
return $this->count;
}
private function limited(int $count): int
{
return $count > self::MAX ? self::MAX : $count;
}
}
// `nvs code order --check` prints nothing for this file, because the class is in order.
Cart $cart = new Cart(12);
// This prints `10`, because the constructor limits the count to `MAX`.
echo $cart->count(), "\n";
// This prints `0`.
echo Cart::empty()->count(), "\n";
Output
10
0

Move a class to another namespace

<?nvs
// Shows a caller after `nvs code rename 'Shop\Cart' 'Shop\Billing\Cart'` moved the class to another namespace.
namespace Shop;
// The command added this `use` line, so this file still writes the short name `Cart`.
use Shop\Billing\Cart;
// The class is now in `billing/Cart.nvs`, in the namespace `Shop\Billing`.
require 'billing/Cart.nvs';
Cart $cart = new Cart();
$cart->add(12);
$cart->add(30);
// This prints `42`.
echo $cart->total(), "\n";
Output
42

Delete unused code

<?nvs
// Shows a class after `nvs code unused` found a method that nothing calls and `nvs code delete` deleted it.
namespace Shop;
// `nvs code unused` printed one line: `Shop\Order::oldTotal`, a method.
// `nvs code delete 'Shop\Order::oldTotal'` deleted the method and its `///` comment.
class Order
{
private int $total = 0;
public function add(int $price): void
{
$this->total += $price;
}
public function total(): int
{
return $this->total;
}
}
// `nvs code unused` now prints nothing for this file.
Order $order = new Order();
$order->add(25);
// This prints `25`.
echo $order->total(), "\n";
Output
25

Inline a variable and a method

<?nvs
// Shows a method after `nvs code inline` replaced a variable and a method call with their values.
namespace Shop;
class Invoice
{
private int $price;
private int $count;
public function constructor(int $price, int $count)
{
$this->price = $price;
$this->count = self::limited($count);
}
// The method had `int $subtotal = $this->price * $this->count;` and returned `$subtotal + 5`.
// `nvs code inline` at `$subtotal` wrote the value in its place and deleted the variable.
public function total(): int
{
return ($this->price * $this->count) + 5;
}
// `nvs code inline 'Shop\Invoice::limited'` would write `$count > 9 ? 9 : $count` in the constructor.
// Then it would delete this method.
private static function limited(int $count): int
{
return $count > 9 ? 9 : $count;
}
}
// Three items at 10 cost 30, plus 5 for shipping. This prints `35`.
echo (new Invoice(10, 3))->total(), "\n";
Output
35

Add a parameter to a method

<?nvs
// Shows the code after `nvs code signature` added a parameter to a method and to every call of it.
namespace Shop;
// The command was `nvs code signature 'Shop\Page::line' 'string $item, int $count' --value '$count=1'`.
// The method now has the parameter `int $count`.
class Page
{
public static function line(string $item, int $count): string
{
return $count . ' x ' . $item;
}
}
// The command wrote the value `1` at each call.
echo Page::line('Lamp', 1), "\n";
echo Page::line('Book', 1), "\n";
// You can now give another count. This prints `3 x Pen`.
echo Page::line('Pen', 3), "\n";
Output
1 x Lamp
1 x Book
3 x Pen

Extract an interface

<?nvs
// Shows a class after `nvs code extract interface 'Shop\Cart' Priced --methods total` wrote an interface.
namespace Shop;
// The interface is in its own file, `parts/Priced.nvs`.
require 'parts/Priced.nvs';
// The command added `implements Priced` to the class. The rest of the class did not change.
class Cart implements Priced
{
private int $total = 0;
public function add(int $price): void
{
$this->total += $price;
}
/// The price of everything in the cart, in cents.
public function total(): int
{
return $this->total;
}
}
// You can now write a method that takes any `Priced` value.
class Receipt
{
public static function describe(Priced $priced): string
{
return 'Total: ' . $priced->total();
}
}
Cart $cart = new Cart();
$cart->add(450);
// This prints `Total: 450`.
echo Receipt::describe($cart), "\n";
Output
Total: 450

Changelog

VersionChange
0.0.5nvs code reads and saves the editor's index file, a list of every name in the project and every place it is used. A call analyses only the files whose content changed, so it is faster. nvs code calls and nvs code reach take several methods in one call.
0.0.5nvs code refs lists a route method passed to a #[Core\Router\Target] parameter as a use of that method.
0.0.6When you rename a property, the name that initProperty, onPropertyGet, onPropertySet and beforePropertySet compare as a string changes too. Find references lists that string.
0.0.6A rename can move a class to another namespace. Write its full new name, such as Shop\Billing\Cart. Every use line changes, and the file moves to the folder that autoload reads for the new name.
0.0.6When you move a class file to another folder with nvs code move or in the editor, the class gets the name that its new path gives. nvs code rename --from <file> does a list of renames in one change.
0.0.6Every rename and move is checked before it changes a file. If the new code would not compile, nothing changes, and you see the error.
0.0.6nvs code unused prints every declaration that nothing in the project uses, and nvs code delete deletes one. nvs code extract, implement, inline and signature do from a terminal what the editor's actions do.
0.0.6Three new editor actions ask you for text first. They change the parameters of a method, move a member to another class, and extract an interface. nvs code signature, nvs code move and nvs code extract interface do the same.