What is PHPStan?

Debug PHP? Aww, hell no!

Not when some kind of bizarro script will do it for you! Check out PHPStan! (Zbornak)

What is PHPStan? PHPStan (as in PHP Standards) checks PHP source code for errors based on the coding standards set forth by the PHP Framework Interoperability Group. (aka. making your Namespaces better with Autoloader). Easily install this static analysis tool for PHP code using the PHP Composer package manager.

PHP-FIG check

If PHPStan finds errors in your code, you should use the feedback it provides to make your code comply with the standards. You may find and eliminate errors in the process. Not only will you improve your code to adhere to best standards, your efficiency might begin to improve. PHPStan provides a report of undefined variables and constants, or functions which aren’t passed the correct amount of arguments. The detection level is adjustable, so you can start out allowing anything-goes, and gradually increase the level to more strict adherences. It’s definitely worth trying!

Install Composer to Try PHPStan

Install PHPStan using PHP Composer. Composer is a package manager for PHP that allows you to easily manage and install dependencies for your project. If you are familiar with Node.js npm, the way Composer works will be familiar to you.

You must provide a valid path to a PHP executable, which Composer will use to install and configure itself to work with your PHP installation. If you’re using WSL or Linux, just type “whereis php” at the command line (without the quotes). If you’re using Xampp on Windows, that path will look something like: C:\Xampp\php\php.exe

Composer provides access to a vast array of useful PHP packages that you can use in your project. These packages can be easily installed by executing a few simple command line statements. By using Composer, you can easily manage dependencies for your PHP project and ensure that all the required packages are installed and up to date.

#TO USE PHPSTAN with WORDPRESS:
#COPY PASTE to BASH term and execute these lines
cd /var/www/html #Go-to #Your #HTDOCS #DOCUMENT_ROOT
composer require --dev phpstan/phpstan 
composer require phpstan/extension-installer
composer require --dev szepeviktor/phpstan-wordpress 
composer show phpstan/extension-installer

Once you get there, if you’re a VS Code user, you probably want to install the PHPStan Visual Studio Code extesnion.

For WordPress developers, there is a PHPStan WordPress extension with rules specifically for the WordPress codebase.

Getting Started

If you like to use print_r() or var_dump() to debug your code

Sometimes, we still want to know more about what’s going on with the code, or just want some notes in the output to help us keep everything in mind. Place the following code inside of a function() that you believe is causing problems (or place it in any function for that matter). Modify it as you see fit. It simply prints to screen some basic info about what is executing where, and in what file.

Manual Debugging Suggestion:

// __FILE__ - is a PHP native Magic Constant. 
// __FILE__ - provides “The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.”
// __LINE__ - The current line number of the file that's processing.
echo '<div class="filename_black">File: '. __FILE__ . ':' .__LINE__.'<br>';
// __FUNCTION__ - The function name, or {closure} for anonymous functions.
echo '<div class="function_red">At function: <span class="bold">' . __FUNCTION__ .'</span>:' .__LINE__ .'</div>';
// pathinfo(__FILE__,PATHINFO_BASENAME) - using the PATHINFO_BASENAME flag, PHP returns just the filename 
// (e.g. /path/to/filename.php will return filename.php)
echo '<div class="basename_green">this->basename - ' . pathinfo(__FILE__, PATHINFO_BASENAME) .':'.__LINE__.'</div>';

Version II:

PHP Classes and Methods:

Note: The __CLASS__ constant is commented out in the HTML below. If you want to view the PHP Class name, remove the <– HTML COMMENTS –> there.

This uses the PHP Native function get_defined_vars(). The key to making use of get_defined_vars() is to recognize the scope from which it is used. Follow the previous link for official documentation. In this example, this code was placed in the method of a class in a custom WordPress plugin to see the value of get_option('plugin_options'), so get_defined_vars() isn’t showing anything outside of what’s set inside of that method. Using get_defined_vars() in the global scope, however, can be too full to be very useful.

You might use this if you’re working on a WordPress plugin or theme, and you want to see the value of the results of register_setting() for example.

$option = get_option('plugin_options');
$filteredoptions = apply_filters('plugin_loaded', get_option('plugin_options'));
        echo '&lt;br&gt;&lt;div class="bold"&gt;file: &lt;/div&gt; : line '.__FILE__.':&lt;strong&gt;'.__LINE__.'&lt;/strong&gt;&lt;br&gt;&lt;strong class="red"&gt;Class&lt;/strong&gt;:: &lt;!-- '.__CLASS__.' --&gt; &lt;span class="green"&gt;method&lt;/span&gt;( &lt;strong class="blue"&gt;'.__METHOD__.' &lt;/strong&gt;); ';
        echo '&lt;br&gt;var_dump(get_defined_vars()): '.var_dump(get_defined_vars());
Expand Your PHP

Whatchu do


Leave a Reply

Your email address will not be published. Required fields are marked *