Nova

Extensions


Readme

PHP formatter for Nova with HTML, Blade and Twig support

This is an extension for Nova to format PHP files on save or by command, it uses phpcs 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
  • 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.

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

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

  • PHP Dedicated server and Dedicated server port PHP is really fast but the start can be a little slow, having a small server runnig will improve the formatting speed, but please try both methods and decide the one you want to use, both are fine.

  • 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

  • Format on Save Enable or disable automatic formatting on save

  • 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.


Changelog

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