### Install Psalm Phar via Composer Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Installs the Psalm Phar executable as a development dependency using Composer. ```bash composer require --dev psalm/phar ``` -------------------------------- ### Install Psalm Plugins Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Installs and enables a Psalm plugin. Plugins provide custom types and enhance Psalm's analysis capabilities for specific libraries. ```bash composer require --dev && vendor/bin/psalm-plugin enable ``` -------------------------------- ### Run Psalm Analysis Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Executes Psalm to analyze your codebase for potential issues. The --no-cache flag ensures a fresh analysis. ```bash ./vendor/bin/psalm --no-cache ``` -------------------------------- ### Install Psalm via Composer Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Installs the latest version of Psalm as a development dependency using Composer. Requires PHP >= 8.2. ```bash composer require --dev vimeo/psalm ``` -------------------------------- ### Generate Psalm Configuration Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Generates a Psalm configuration file for your project. This file is used to customize Psalm's behavior. ```bash ./vendor/bin/psalm --init ``` -------------------------------- ### Download and Run Psalm Phar Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Downloads the Psalm Phar (PHP Archive) executable, makes it executable, and runs it to check its version. The Phar is a self-contained version useful for avoiding dependency conflicts. ```bash wget https://github.com/vimeo/psalm/releases/latest/download/psalm.phar chmod +x psalm.phar ./psalm.phar --version ``` -------------------------------- ### Install Psalm Plugin Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/plugins/using_plugins.md Installs a Psalm plugin as a development dependency using Composer. ```bash composer require --dev ``` -------------------------------- ### Basic Psalm Configuration Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/configuration.md A minimal example of a Psalm configuration file (`psalm.xml`) specifying the project's source directories. ```xml ``` -------------------------------- ### PHP Hello World Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UnusedBaselineEntry.md A simple PHP script that defines and echoes a string variable. This serves as a basic example for PHP code. ```php ``` -------------------------------- ### PHP Hello World Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UnusedIssueHandlerSuppression.md A basic PHP snippet demonstrating string assignment and output. This is a standard example of PHP syntax. ```php ``` -------------------------------- ### Run Psalm using Docker Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/installation.md Runs Psalm within an official Docker image, which can provide a significant performance boost compared to running directly on local PHP. Mounts the current directory to /app inside the container. ```bash docker run -v $PWD:/app --rm -it ghcr.io/danog/psalm:latest /composer/vendor/bin/psalm --no-cache ``` -------------------------------- ### Basic Psalm XML Configuration Source: https://github.com/vimeo/psalm/wiki/Configuration A minimal example of a Psalm XML configuration file, specifying the project files to be analyzed. ```xml ``` -------------------------------- ### Example Taint Sources in PHP Source: https://github.com/vimeo/psalm/blob/6.x/docs/security_analysis/index.md Demonstrates common sources of user-controlled input that Psalm's taint analysis tracks, such as data from GET, POST, and COOKIE superglobals. ```php $_GET['id'] $_POST['email'] $_COOKIE['token'] ``` -------------------------------- ### Manage Psalm Plugins Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/plugins/using_plugins.md Commands for managing installed Psalm plugins. This includes enabling, disabling, and showing the status of local plugins. ```APIDOC psalm-plugin enable - Enables a specific Psalm plugin. psalm-plugin disable - Disables a specific Psalm plugin. psalm-plugin show - Displays a list of all local Psalm plugins, indicating whether they are enabled or disabled. ``` -------------------------------- ### Install Vimeo Psalm with Composer Source: https://github.com/vimeo/psalm/wiki/Installation Installs the latest development version of Vimeo Psalm as a dev dependency using Composer. It also runs composer install to ensure all project dependencies are met. ```bash > composer require --dev "vimeo/psalm:dev-master" > composer install ``` -------------------------------- ### Psalm Configuration with XInclude Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/configuration.md An example demonstrating how to use XInclude to split Psalm configuration across multiple XML files, referencing `files.xml`. ```xml ``` -------------------------------- ### Psalm Community Support Examples Source: https://github.com/vimeo/psalm/blob/6.x/docs/contributing/what_makes_psalm_complicated.md Illustrates how Psalm supports the PHP community through formal and informal PHPDoc annotations, and by supporting non-Composer projects like WordPress. ```php // Supporting formal PHPDoc annotations // Supporting informal PHPDoc annotations like ArrayIterator|string[] // Supporting non-Composer projects (e.g., WordPress) ``` -------------------------------- ### Psalm Configuration File Inclusion Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/configuration.md An example of a configuration file (`files.xml`) that can be included in the main `psalm.xml` using XInclude, specifying individual files. ```xml ``` -------------------------------- ### Basic MyContainer Class Source: https://github.com/vimeo/psalm/blob/6.x/docs/annotating_code/templated_annotations.md A simple PHP class 'MyContainer' with a constructor and a getter method. This serves as a basic example before introducing templating. ```php value = $value; } public function getValue() { return $this->value; } } ``` -------------------------------- ### MixedMethodCall Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/MixedMethodCall.md Demonstrates the MixedMethodCall error when calling a method on a value with an unknown type after array_pop. ```php foo(); // MixedMethodCall emitted here } callFoo( [new A()] ); ``` -------------------------------- ### UndefinedMagicPropertyFetch Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UndefinedMagicPropertyFetch.md Demonstrates the UndefinedMagicPropertyFetch error when accessing a non-existent magic property. ```php foo; ?> ``` -------------------------------- ### UndefinedPropertyAssignment Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UndefinedPropertyAssignment.md This code snippet demonstrates the UndefinedPropertyAssignment issue. It shows an instance of class 'A' being created, and then a new property 'foo' is assigned to it, which was not defined in the class. ```php foo = "bar"; ``` -------------------------------- ### Unsafe Instantiation Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UnsafeInstantiation.md Demonstrates the UnsafeInstantiation error when a child class with a different constructor signature extends a parent class that uses `new static`. ```php Usage with array_keys Source: https://github.com/vimeo/psalm/blob/6.x/docs/annotating_code/type_syntax/utility_types.md Demonstrates the `key-of` utility type in Psalm, which returns the offset-type for array types. This example shows how to use it with `array_keys` to get a list of keys from a generic array type. ```php /** * @template T of array * @param T $array * @return list> */ function getKeys($array) { return array_keys($array); } ``` -------------------------------- ### Running Psalm Language Server in Docker Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/language_server.md Example command for executing the Psalm language server within a Docker container, demonstrating how to map project folders and specify the server's root directory. ```bash docker-compose exec php /usr/share/php/psalm/psalm-language-server \ -r=/var/www/html \ --map-folder=/var/www/html:$PWD ``` -------------------------------- ### Create Psalm Plugin Skeleton Project Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/plugins/authoring_plugins.md This command bootstraps a new Psalm plugin project using the skeleton repository. It's essential to adjust namespaces in composer.json, Plugin.php, and the tests folder after creation. ```bash composer create-project weirdan/psalm-plugin-skeleton:dev-master your-plugin-name ``` -------------------------------- ### PHP Header Injection Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/TaintedHeader.md This snippet demonstrates a common scenario for header injection in PHP, where user input from the 'header' GET parameter is directly used in the `header()` function. This can lead to various security issues if the input is not properly sanitized. ```php ./vendor/bin/psalm somefile.php ERROR: InvalidArgument - somefile.php:3 - Argument 1 of implode expects `string`, `array` provided ``` -------------------------------- ### PHP jsonSerialize Method Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/MethodSignatureMustProvideReturnType.md Example of a class implementing JsonSerializable with the jsonSerialize method. This method should ideally declare a return type in PHP 8.1+ to avoid deprecation notices. ```php 'A']; } } ``` -------------------------------- ### Psalm Command-line Options Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/command_line_usage.md Displays all available command-line options for Psalm. This is useful for understanding the full range of configurations and functionalities. ```bash ./vendor/bin/psalm --help ``` -------------------------------- ### Union Type Example with Ternary Expression Source: https://github.com/vimeo/psalm/blob/6.x/docs/annotating_code/type_syntax/union_types.md Demonstrates how a variable can be typed using a union type when its value can be one of several types, as shown in this ternary expression example. ```php Specifies the path to the configuration file. Defaults to ./psalm.xml. --monochrome Disables colored output for a monochrome display. --show-info=[BOOLEAN] Controls whether non-error parser findings are displayed. Accepts true or false. --diff Analyzes only files that have changed since the last successful run, along with their dependents. --self-check Performs a self-check of Psalm, useful for development or updates. --output-format= Sets the output format to either JSON or console (default text). --find-dead-code Enables the detection of dead code within the project. --find-references-to= Searches the codebase for references to a specified fully-qualified class or method (format: class::methodName). --threads= Runs Psalm's analysis using multiple threads. Defaults to 1 thread. ``` -------------------------------- ### PHP Example of Unused Class Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/UnusedClass.md This snippet demonstrates a scenario where Psalm might flag classes A and B as unused if they are not referenced elsewhere in the code. The example shows the declaration of two final classes, A and B, and an instantiation of class A. ```php [ 0 => 'bool', 'version1' => 'string', 'version2' => 'string', 'operator' => '\'!=\'|\'<'|\'<=\'|\'<>\'|\'=\'|\'==\'|\'>\'|\'>=\'|\'eq\'|\'ge\'|\'gt\'|\'le\'|\'lt\'|\'ne\'|null', ], 'version_compare\'1' => [ 0 => 'int', 'version1' => 'string', 'version2' => 'string', ], ] ``` -------------------------------- ### Running Psalter via Binary Source: https://github.com/vimeo/psalm/blob/6.x/docs/manipulating_code/fixing.md Demonstrates how to execute the Psalter tool directly using its binary file. This is a common way to invoke the code fixing utility. ```bash vendor/bin/psalter [args] ``` -------------------------------- ### PHP Return Type Example with Psalm Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/configuration.md Demonstrates how Psalm's `restrictReturnTypes` feature flags a mismatch between an inferred literal return type and a declared broader type. It also shows how to fix this by specifying a more precise return type in the docblock. ```php function getOne(): int // declared type: int { return 1; // inferred type: 1 (int literal) } /** * @return 1 */ function getOne(): int { return 1; } ``` -------------------------------- ### Redundant Function Call Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/RedundantFunctionCallGivenDocblockType.md Demonstrates a scenario where function calls like array_values and strtolower are considered redundant due to type information provided in the docblock. The example shows how Psalm can infer types from docblocks to identify potentially unnecessary operations. ```php } $s * * @return lowercase-string */ function foo($s): string { $redundantList = array_values($s); $redundantSubList = array_values($s[1]); $redundantLowercase = strtolower($redundantSubList[0]); return $redundantLowercase; } ``` -------------------------------- ### psalm-review via Phar Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/command_line_usage.md Executes the review functionality using the main psalm entry point, particularly useful when working with the Phar archive. This allows for IDE-based issue review with optional filtering. ```bash ./vendor/bin/psalm.phar --review report.json code|phpstorm|code-server [ inv|rev|[~-]IssueType1 ] [ [~-]IssueType2 ] ... ``` -------------------------------- ### Psalm Project Settings Source: https://github.com/vimeo/psalm/wiki/Configuration Details on project-level configuration elements within the Psalm XML file, including file extensions, plugins, issue handling, mock classes, and stubs. ```xml ``` -------------------------------- ### Interface Instantiation Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/InterfaceInstantiation.md This code snippet demonstrates the scenario that triggers the InterfaceInstantiation issue in Psalm. It shows an attempt to create a new instance of an interface, which is not allowed in PHP. ```php foo(str: "hello"); } ``` -------------------------------- ### Psalm Project Files Configuration Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/configuration.md Defines the directories and files that Psalm should inspect for analysis. It includes options to specify directories to include and directories to ignore, with an option to allow missing ignored directories. ```xml ``` -------------------------------- ### Vim vim-lsp Configuration for Psalm Language Server Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/language_server.md Configuration for the vim-lsp plugin in Vim to register and use the Psalm language server. ```vim au User lsp_setup call lsp#register_server({ \'name': 'psalm-language-server', \'cmd': {server_info->[expand('vendor/bin/psalm-language-server')]}, \'allowlist': ['php'], \}) ``` -------------------------------- ### PHP String Increment Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/StringIncrement.md Demonstrates the behavior of incrementing a string in PHP, which triggers the StringIncrement event in Psalm. ```php foo = new stdClass(); ``` -------------------------------- ### Psalm Example Output Source: https://github.com/vimeo/psalm/blob/6.x/docs/README.md Demonstrates the typical error output from Psalm when analyzing a PHP file. It highlights an `InvalidArgument` error related to function arguments, including the file, line, column, error type, and a link to further information. ```bash > ./vendor/bin/psalm implode_strings.php ERROR: InvalidArgument - somefile.php:3:14 - Argument 1 of implode expects `string`, `array` provided (see https://psalm.dev/004) ``` -------------------------------- ### Emacs Eglot Configuration for Psalm Language Server Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/language_server.md Configuration for Emacs using the eglot package to integrate with the Psalm language server. It specifies the command to run the server and sets up advice for better hover information. ```emacs-lisp (when (file-exists-p "vendor/bin/psalm-language-server") (progn (require 'php-mode) (require 'eglot) (add-to-list 'eglot-server-programs '(php-mode . ("php" "vendor/bin/psalm-language-server"))) (add-hook 'php-mode-hook 'eglot-ensure) (advice-add 'eglot-eldoc-function :around (lambda (oldfun) (let ((help (help-at-pt-kbd-string))) (if help (message "%s" help) (funcall oldfun))))) ) ) ``` -------------------------------- ### PHP Array Examples Source: https://github.com/vimeo/psalm/blob/6.x/docs/annotating_code/type_syntax/array_types.md Demonstrates the common ways arrays are used in PHP: as lists, associative arrays, and makeshift structs. ```php 'hello', 5 => 'goodbye']; $b = ['a' => 'AA', 'b' => 'BB', 'c' => 'CC']; ``` ```php 'Psalm', 'type' => 'tool']; ``` -------------------------------- ### psalm-review Tool Usage Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/command_line_usage.md Launches the psalm-review tool to manually review issues one by one in a specified IDE. It parses a Psalm JSON report and allows filtering issues by type. ```bash ./vendor/bin/psalm-review report.json code|phpstorm|code-server [ inv|rev|[~-]IssueType1 ] [ [~-]IssueType2 ] ... ``` -------------------------------- ### Direct Type Instance Creation Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/plugins/plugins_type_system.md Demonstrates direct instantiation of Psalm's type objects for literal strings, floats, and named objects. ```php new TLiteralString('A text string'); new TLiteralFloat(3.142); new TNamedObject('Foo\Bar\SomeClass'); ``` -------------------------------- ### ReferenceReusedFromConfusingScope Example Source: https://github.com/vimeo/psalm/blob/6.x/docs/running_psalm/issues/ReferenceReusedFromConfusingScope.md Demonstrates the 'ReferenceReusedFromConfusingScope' issue where reusing a variable that was a reference in a loop leads to unexpected modification of the original array. ```php