Nova

Extensions


Readme

Nova PHP formatter

This is an extension for Nova Editor to format PHP files on save or by command, it uses PHP CS Fixer to format PHP code, it can also format HTML inside PHP files, Blade and Twig files.

Features

  • Works out of the box no need to install anything (at least for Big Sur users and lower, Mac OS Monterey and Up, read below)
  • HTML support, it can handle files with inline HTML
  • Blade support, it can format .blade.php files
  • Twig support, it can format .twig files
  • Many options to customize the formatter the way you want
  • Format on save, using a command or right click
  • Workspace and Global configuration available
  • Support for .editorconfig

 

Before Start

If you are using Prettier by Alexander Weiss make sure to go to Prettier Preferences and in the section Ignored Syntaxes select PHP.

 

Mac OS Monterey+ requirements

Starting from Mac OS Monterey Apple removed PHP so you have to install it manually, that can easily be done with Homebrew, just open your terminal and paste the commands below:

1.- Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2.- Install PHP

brew install php

That's it, PHP is now installed, now go to the extension preferences and in "PHP path" click the "choose" button so you can select the PHP executable. Homebrew installs PHP in /usr/local/Cellar/php/[ your php version ]/bin/php

 

Need help configuring the extension?

This extension uses phpcs fixer under the hood, if you try to format your code and don't see any change then there's a big chance you are not setting your rules correctly, here you can see the recommended way for defining your fixer rules phpcsfixer-rules.md

 

Extension Options

This extension provides multiple options to help you customize the formatting of your code

 

PHP CS Fixer Options

  • PHP Path - Path to your PHP installation, if empty the default PHP included with the OS will be used (Only for Big Sur and lower, Mac OS Monterey and up does no longer includes PHP by default)

  • CS Fixer Path Path to php-cs-fixer, if empty the fixer included with the extension will be used

  • Coding Standard Select the coding standard you prefer, available options are "PSR1", "PSR2", "PSR12", "Symfony", "PhpCsFixer", "WordPress", "None" If you set it to None you can add any other standard you want in the Additional Fixer Rules. WordPress Fixes are not included with phpcs fixer so if you select WordPress as standard the Additional Fixer Rules option will be ignored and the formatter will use a defined set of rules for WordPress.

  • Additional Fixer Rules You can configure rules to format PHP the way you want, this rules will be global but can be overwritten by using a config file or configuring the extension in your workspace, you can view all available options here PHP-CS-Fixer:

You can define your rules like this (one per line)

array_syntax: { "syntax": "short" }
array_indentation: true
  • Global config path Instead of defining the rules above you can select a global configuration file ".php_cs or .php_cs.dist", the fixer will use your configuration file instead of the global rules. You can also overwritte the global config file by adding a ".php_cs or .php_cs.dist" file in the root of your project that way each project can have it's own rules or if you don't want the file inside your project you can define it in the extension options of the active workspace. Need help with your configuration file?

  • Format on Save Enable or disable automatic formatting on save

  • Format on save only if workspace has a config file Only format on save if the workspace contains a configuration file for example: .php_cs or .php_cs.dist or .php-cs-fixer.php or .php-cs-fixer.dist.php. You can still format the file doing right click in the code and choose Format with PHP CS Fixer

  • Ignore remote files Enable or disable automatic formatting of remote files

  • Respect Nova preferences for PHP indentation If enabled, the formatter will use Nova's tab width and indentetation style for the language PHP which also adds supoprt for .editorconfig

  • Use tabs Enable to indent lines with tabs instead of spaces

  • Tab width Specify the number of spaces per indentation-level.

 

HTML Options

  • Format HTML in PHP files Enable or disable formatting of HTML inside the PHP files.

  • Apply Additional fixes Addition HTML fixes, more info here aditional-fixes.md

  • HTML Rules Add the rules you want to use to format your HTML, here's an example of the available options: Note: Please read and make sure you understand the options otherwise do not move anything here, check all the available options here html-options.md

max_preserve_newlines: 10
preserve_newlines: true
indent_scripts: "keep"

 

Blade Options

  • Format Blade files on save Enable or disable format on save for blade files .blade.php

  • Respect Nova preferences for Blade indentation If enabled, the formatter will use Nova's tab width and indentetation style for Blade files which also adds supoprt for .editorconfig

  • Use Tabs Set to true to use tabs or leave unchecked to use spaces

  • Tab Width Specify the number of spaces per indentation level

  • Blade Rules Add the rules you want to use to format your Blade, it can use the same options as the HTML Rules option

max_preserve_newlines: 10
preserve_newlines: true
indent_scripts: "keep"

 

Twig Options

  • Format Twig files on save Enable or disable format on save for twig files .twig

  • Respect Nova preferences for Twig indentation If enabled, the formatter will use Nova's tab width and indentetation style for Blade files which also adds supoprt for .editorconfig

  • Use Tabs Set to true to use tabs or leave unchecked to use spaces

  • Tab Width Specify the number of spaces per indentation level

  • Twig Rules Add the rules you want to use to format your Twig file, for more information about the options see twig-options.md

preserve: 3
new_line: true

 

Help us improve the extension

There's a link to submit bug reports, if you find an error please let us know, it takes you a few minutes.

This extension is made with love by biati digital


Changelog

Version 4.3.1

  • Fixed: Problems with latest Nova version
  • Improved: Improved compatibility with newer versions of php-cs-fixer
  • Improved: Added PHP_CS_FIXER_IGNORE_ENV to avoid errors with multiple PHP versions

Version 4.3.0

  • Updated: PHP CS Fixer 3.8.0

Version 4.2.3

  • Fixed: Another error in Blade formatter and double curly braces
  • Updated: README

Version 4.2.2

  • Fixed: Error in Blade formatter and double curly braces

Version 4.2.1

  • Updated: Server file
  • Updated: Server formatting code
  • Fixed: Error in Nova 9+

Version 4.2.0

  • New: Added env variable PHP_CS_FIXER_IGNORE_ENV
  • Updated: PHP CS Fixer 3.8.0
  • Updated: Readme
  • Updated: Formatter support for server mode is deprecated, full support will be removed in the future

Version 4.1.0

  • Updated: PHP-CS-Fixer to 3.4.0

Version 4.0.2

  • Fixed: Creating temp files on file paths that contain numbers.
  • Updated: PHP path option now is a path select field
  • Updated: README

Version 4.0.1

  • New: New extension icon
  • Updated: README

Version 4.0.0

IMPORTANT: This update includes php-cs-fixer 3.0.2 (need to be enabled in the extension preferences). If you have a custom configuration please check the Upgrade Guide. php-cs-fixer-2-x is no longer maintained and will be removed in the near future so make sure to enable v3.

  • Updated: PHP CS Fixer to 2.19.2 (will soon be removed as it's no longer maintained)
  • Added: PHP CS Fixer 3.1.0 River For now you need to enable V3 in the extension preferences and test your configuration, V2 will be removed in the near future so you have time to make the change
  • Added: New option "Enable PHP CS Fixer 3" You might need restart
  • Added: New option "Format on save only if workspace has a config file"
  • Fixed: Twig not respecting custom user rules
  • Fixed: PHP CS Fixer additional rules not working
  • Updated: js-beautify to 1.14.0

Version 3.1.0

  • New: Files are cached by php-cs-fixer to improve formatting speed
  • New: Added new option Format on save only if workspace has a config file
  • Fixed: Improved HTML indent_with_tabs setting

Version 3.0.2

  • Fixed: Blade @Page tag in Blade -> DomPDF
  • Fixed: Blade @if in HTML tag

Version 3.0.1

  • Improved: handling previous preferences

Version 3.0.0

  • New: Added new option to select the coding standard
  • New: Added WordPress standard with custom rules
  • New: added support for the extension advphp
  • New: Now you can configure the extension to respect Nova's preference for indentation, this also adds support for .editorconfig
  • New: The formatter now updates tabLength and softTabs in the active editor if Nova automatically detects a different and incorrect tabLength
  • New: Let Nova restore the cursor position/selection after formatting
  • Improved: Formatting speed on large PHP files
  • Improved: Indentation in certain files with mixed PHP and HTML
  • Updated: PHP CS Fixer to 2.18.3

Version 2.0.5

  • Fixed: Blade @php and @endphp blocks not indented correctly

Version 2.0.4

  • Minor improvements

Version 2.0.3

  • Fixed: Blade comments
  • Fixed: PHP inside HTML script tags

Version 2.0.2

  • Improved: Blade debug logs to display more information

Version 2.0.1

  • Fixed: Indentation error in blade files for elseif|else inside if blocks

Version 2.0.0

  • New: Added support for blade files
  • New: Added support for twig files
  • New: Added support for workspace configuration
  • New: New HTML additional fixes
  • Fixed: Indentation error in PHP files with HTML and additional fixes enabled
  • Code updates

Version 1.0.3

  • Removed hack to autosave the file when formatting is done, Requires Nova v2 as it includes a fix for this so the hack is no longer necessary

Version 1.0.2

  • Updated: Added more HTML additional fixes
  • Updated: Added funding link, any amount is appreciated and it helps me to continue the development of the extension

Version 1.0.1

  • New: Added new option for HTML "Apply Additional fixes" to imrpove even more the formatting when using PHP and HTML in the same file
  • New: Added new option to ignore format on save for remote files
  • New: Do nothing if the formatted text and the original text are the same, no need to update the editor or resave the file
  • New: Do cleanup on deactivate event, this will remove all temp files generated in the workspaceStoragePath directory
  • Updated: Changed Format HTML in PHP files to active by default
  • Updated: Added link in the extension preferences to view all the available php-cs-fixer options
  • Updated: Added link in the extension preferences to view all the additional HTML/PHP fixes

Version 1.0

Initial release