### Install plugins with Composer Source: https://psalm.dev/docs/running_psalm/installation Installs a Psalm plugin using Composer and enables it. ```bash composer require --dev && vendor/bin/psalm-plugin enable ``` -------------------------------- ### Install Psalm using Composer Source: https://psalm.dev/docs/running_psalm/installation Installs the latest version of Psalm using Composer. ```bash composer require --dev vimeo/psalm ``` -------------------------------- ### Install Psalm Phar using Composer Source: https://psalm.dev/docs/running_psalm/installation Installs the Psalm Phar executable as a development dependency using Composer. ```bash composer require --dev psalm/phar ``` -------------------------------- ### UnresolvableConstant Example Source: https://psalm.dev/docs/running_psalm/issues/UnresolvableConstant Example demonstrating an unresolvable constant in Psalm. ```php */ public function bar(): string { return self::BAR[0]; } } ``` -------------------------------- ### List example Source: https://psalm.dev/docs/annotating_code/type_syntax/array_types Example of a list in PHP. ```php foo; } ``` -------------------------------- ### UnusedProperty Example Source: https://psalm.dev/docs/running_psalm/issues/UnusedProperty Example demonstrating an unused private property. ```php foo; } } $a = new A(); echo $a->getFoo(); ``` -------------------------------- ### Example for @seal-properties Source: https://psalm.dev/docs/annotating_code/supported_annotations Demonstrates how @seal-properties instructs Psalm to disallow getting and setting properties not explicitly defined. ```php bar = 5; // this call fails ``` -------------------------------- ### InternalClass example Source: https://psalm.dev/docs/running_psalm/issues/InternalClass Example demonstrating the InternalClass issue. ```php ``` -------------------------------- ### projectFiles configuration example Source: https://psalm.dev/docs/running_psalm/configuration Example of configuring project files and directories to ignore. ```xml ``` -------------------------------- ### Example Plugin Implementation Source: https://psalm.dev/docs/running_psalm/plugins/authoring_plugins A basic example of a Psalm plugin implementing the AfterStatementAnalysisInterface. ```php array('default' => 'world.com'), 'flags' => FILTER_NULL_ON_FAILURE)); ``` -------------------------------- ### enableExtensions configuration example Source: https://psalm.dev/docs/running_psalm/configuration Example of enabling specific PHP extensions for Psalm. ```xml ``` -------------------------------- ### PossiblyNullPropertyFetch Example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyNullPropertyFetch Example demonstrating the PossiblyNullPropertyFetch issue in Psalm. ```php foo; } ``` -------------------------------- ### DirectConstructorCall example Source: https://psalm.dev/docs/running_psalm/issues/DirectConstructorCall Example showing a direct call to __construct() which is incorrect. ```php __construct(); // wrong ``` -------------------------------- ### UnusedMethodCall Example Source: https://psalm.dev/docs/running_psalm/issues/UnusedMethodCall Example demonstrating an unused method call. ```php foo = $foo; } public function getFoo() : string { return $this->foo; } } $a = new A("hello"); $a->getFoo(); ``` -------------------------------- ### stubs configuration example Source: https://psalm.dev/docs/running_psalm/configuration Example of configuring stub files for classes not visible to Psalm. ```xml ``` -------------------------------- ### Example output Source: https://psalm.dev/docs An example of Psalm's output when analyzing a PHP file. ```php ./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) ``` -------------------------------- ### RawObjectIteration example Source: https://psalm.dev/docs/running_psalm/issues/RawObjectIteration Example of iterating over an object's properties. ```php foo(); } private function foo() : void {} private function bar() : void {} } $a = new A(); ``` -------------------------------- ### Example Sources Source: https://psalm.dev/docs/security_analysis Examples of user-controlled input that can be considered taint sources. ```php $_GET[‘id’] $_POST['email'] $_COOKIE['token'] ``` -------------------------------- ### IfThisIsMismatch Example Source: https://psalm.dev/docs/running_psalm/issues/IfThisIsMismatch Example demonstrating the IfThisIsMismatch issue in Psalm. ```php data = $data; } /** * @psalm-if-this-is a */ public function test(): void { } } $i = new a(123); $i->test(); $i = new a("test"); // IfThisIsMismatch - Class is not a as required by psalm-if-this-is $i->test(); ``` -------------------------------- ### RiskyCast Example Source: https://psalm.dev/docs/running_psalm/issues/RiskyCast Example of casting an array to an integer, which Psalm flags. ```php ``` -------------------------------- ### UndefinedPropertyFetch Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedPropertyFetch Example of getting a property on an object that does not have that property defined. ```php foo; ``` -------------------------------- ### Run Psalm Source: https://psalm.dev/docs/running_psalm/installation Executes Psalm to scan the project for issues. ```bash ./vendor/bin/psalm --no-cache ``` -------------------------------- ### Associative array examples Source: https://psalm.dev/docs/annotating_code/type_syntax/array_types Examples of associative arrays in PHP. ```php 'hello', 5 => 'goodbye']; $b = ['a' => 'AA', 'b' => 'BB', 'c' => 'CC']; ``` -------------------------------- ### psalm-review with Phar Source: https://psalm.dev/docs/running_psalm/command_line_usage Running the psalm-review tool using the main psalm entry point, useful when working with the phar. ```bash ./vendor/bin/psalm.phar --review report.json code|phpstorm|code-server [ inv|rev|[~-]IssueType1 ] [ [~-]IssueType2 ] ... ``` -------------------------------- ### Union Type Examples Source: https://psalm.dev/docs/running_psalm/plugins/plugins_type_system Examples of creating Union types with Atomic types. ```php new Union([new TNamedObject('Foo\Bar\SomeClass')]); // equivalent to Foo\Bar\SomeClass in docblock new Union([new TString(), new TInt()]); // equivalent to string|int in docblock ``` -------------------------------- ### DuplicateParam example Source: https://psalm.dev/docs/running_psalm/issues/DuplicateParam Example of a function with a duplicate parameter definition. ```php ``` -------------------------------- ### OverriddenPropertyAccess Example Source: https://psalm.dev/docs/running_psalm/issues/OverriddenPropertyAccess Example showing a property with less accessibility in a child class than in its parent. ```php ``` -------------------------------- ### InvalidScalarArgument Example Source: https://psalm.dev/docs/running_psalm/issues/InvalidScalarArgument Example demonstrating when InvalidScalarArgument is emitted. ```php ``` -------------------------------- ### MethodSignatureMustOmitReturnType example Source: https://psalm.dev/docs/running_psalm/issues/MethodSignatureMustOmitReturnType Example of a `__clone` method defined with a return type. ```php ``` -------------------------------- ### Basic Example Source: https://psalm.dev/docs/running_psalm/issues/MixedArgumentTypeCoercion This example shows a basic case where Psalm might emit a MixedArgumentTypeCoercion error. ```php foo; } } ``` -------------------------------- ### Running the server in a docker container Source: https://psalm.dev/docs/running_psalm/language_server Example command for running Psalm's language server within a Docker container. ```bash docker-compose exec php /usr/share/php/psalm/psalm-language-server \ -r=/var/www/html \ --map-folder=/var/www/html:$PWD ``` -------------------------------- ### RiskyTruthyFalsyComparison Example Source: https://psalm.dev/docs/running_psalm/issues/RiskyTruthyFalsyComparison Example of a risky truthy/falsy comparison in PHP. ```php Example Source: https://psalm.dev/docs/running_psalm/plugins/plugins_type_system Shows an example of an array treated as a mapping between string keys and integer values. ```php $a = []; foreach (range(1,1) as $_) $a[(string)rand(0,1)] = rand(0,1); // array ``` -------------------------------- ### ImpureMethodCall Example Source: https://psalm.dev/docs/running_psalm/issues/ImpureMethodCall Example demonstrating an impure method call within a pure function. ```php a++; } } /** @psalm-pure */ function filterOdd(int $i, A $a) : ?int { $a->foo(); if ($i % 2 === 0 || $a->a === 2) { return $i; } return null; } ``` -------------------------------- ### MixedPropertyAssignment Example Source: https://psalm.dev/docs/running_psalm/issues/MixedPropertyAssignment Example of assigning a property to a value for which Psalm cannot infer a type. ```php foo = "bar"; } ``` -------------------------------- ### UndefinedPropertyAssignment Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedPropertyAssignment Example of assigning a property to an object that does not have that property defined. ```php foo = "bar"; ``` -------------------------------- ### InvalidStringClass Example Source: https://psalm.dev/docs/running_psalm/issues/InvalidStringClass Example demonstrating the InvalidStringClass issue. ```php ``` -------------------------------- ### DeprecatedInterface example Source: https://psalm.dev/docs/running_psalm/issues/DeprecatedInterface Example of referring to a deprecated interface. ```php foo = 5; ``` -------------------------------- ### ImpureStaticProperty Example Source: https://psalm.dev/docs/running_psalm/issues/ImpureStaticProperty Example demonstrating the use of a static property within a pure method. ```php ``` -------------------------------- ### DeprecatedMethod example Source: https://psalm.dev/docs/running_psalm/issues/DeprecatedMethod Example of calling a deprecated method. ```php foo(); ``` -------------------------------- ### Plugin Configuration with Absolute Path Source: https://psalm.dev/docs/running_psalm/plugins/authoring_plugins Example of how to add a plugin to the Psalm configuration file using an absolute path. ```xml ``` -------------------------------- ### Run Psalm using Docker Source: https://psalm.dev/docs/running_psalm/installation Runs Psalm within the official Docker image for improved performance. ```bash docker run -v $PWD:/app --rm -it ghcr.io/danog/psalm:latest /composer/vendor/bin/psalm --no-cache ``` -------------------------------- ### Basic Psalm Execution Source: https://psalm.dev/docs/running_psalm/command_line_usage Runs Psalm on all files in the project referenced by . ```bash ./vendor/bin/psalm ``` -------------------------------- ### UndefinedInterface Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedInterface Example of an interface referencing a non-existent class. ```php and list in Psalm. ```php $arr */ function takesArray(array $arr) : void { if ($arr) { // this index may not be set echo $arr[0]; } } /** * @psalm-param list $arr */ function takesList(array $arr) : void { if ($arr) { // list indexes always start from zero, // so a non-empty list will have an element here echo $arr[0]; } } takesArray(["hello"]); // this is fine takesArray([1 => "hello"]); // would trigger bug, without warning takesList(["hello"]); // this is fine takesList([1 => "hello"]); // triggers warning in Psalm ``` -------------------------------- ### UndefinedInterfaceMethod Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedInterfaceMethod Example of calling a method that does not exist on an interface. ```php bar(); } ``` -------------------------------- ### Configuration file split using XInclude Source: https://psalm.dev/docs/running_psalm/configuration An example of splitting the configuration into multiple files using XInclude. ```xml ``` -------------------------------- ### TaintedUserSecret Example Source: https://psalm.dev/docs/running_psalm/issues/TaintedUserSecret Example demonstrating the TaintedUserSecret issue in Psalm. ```php getPassword(); } ``` -------------------------------- ### TaintedUnserialize Example Source: https://psalm.dev/docs/running_psalm/issues/TaintedUnserialize Example of tainted input to an unserialize call. ```php name = $name; } } /** * @psalm-taint-specialize */ function echoUserName(User $user) { echo $user->name; // Error, detected tainted input } $user1 = new User("Keith"); $user2 = new User($_GET["name"]); echoUserName($user1); ?> ``` -------------------------------- ### PossiblyInvalidPropertyAssignment Example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyInvalidPropertyAssignment Example demonstrating the PossiblyInvalidPropertyAssignment issue in Psalm. ```php bar = "5"; ``` -------------------------------- ### Keyed Array Example Source: https://psalm.dev/docs/running_psalm/plugins/plugins_type_system Demonstrates a keyed array (object-like array) with known keys and optional values. ```php $x = ["a" => 1, "b" => 2]; // is TKeyedArray, array{a: int, b: int} $y = rand(0, 1) ? ["a" => null] : ["a" => 1, "b" => "b"]; // is TKeyedArray with optional keys/values, array{a: ?int, b?: string} ``` -------------------------------- ### NullPropertyFetch Example Source: https://psalm.dev/docs/running_psalm/issues/NullPropertyFetch Example demonstrating an attempt to fetch a property on a null value, which triggers the NullPropertyFetch issue. ```php foo; ``` -------------------------------- ### PossiblyUndefinedStringArrayOffset example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyUndefinedStringArrayOffset Example of a string-keyed offset not checked for existence. ```php $arr */ function foo(array $arr) : void { echo $arr["hello"]; } ``` -------------------------------- ### Basic example of MixedReturnTypeCoercion Source: https://psalm.dev/docs/running_psalm/issues/MixedReturnTypeCoercion This example shows a basic case where Psalm might emit the MixedReturnTypeCoercion issue. ```php foo = strpos($s, "haystack"); } ``` -------------------------------- ### NonStaticSelfCall Example Source: https://psalm.dev/docs/running_psalm/issues/NonStaticSelfCall Example of calling a non-static function statically. ```php foo = "bar"; ``` -------------------------------- ### Invalid Scope Example Source: https://psalm.dev/docs/running_psalm/issues/InvalidScope Example of referring to `$this` outside a class. ```php ``` -------------------------------- ### InvalidDocblockParamName example Source: https://psalm.dev/docs/running_psalm/issues/InvalidDocblockParamName Example of an invalid docblock param name. ```php ``` -------------------------------- ### Makeshift Structs example Source: https://psalm.dev/docs/annotating_code/type_syntax/array_types Example of a makeshift struct in PHP. ```php 'Psalm', 'type' => 'tool']; ``` -------------------------------- ### Undefined Variable Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedVariable Example of referencing an undefined variable in PHP. ```php ``` -------------------------------- ### Specializing taints in functions (using @psalm-pure) Source: https://psalm.dev/docs/security_analysis/avoiding_false_positives Example showing that `@psalm-pure` functions also specialize taint tracking. ```php ``` -------------------------------- ### Undefined Constant Example Source: https://psalm.dev/docs/running_psalm/issues/UndefinedConstant Example of referencing an undefined constant in PHP. ```php xpath($expression); } ``` -------------------------------- ### respectIncludeOnce Source: https://psalm.dev/docs/running_psalm/configuration Specifies whether Psalm should respect `require_once()` and `include_once()` behavior, processing files only after their first inclusion. Defaults to `false`, meaning Psalm treats them like `require`/`include` to check all permutations. ```xml ``` -------------------------------- ### TaintedEval Example Source: https://psalm.dev/docs/running_psalm/issues/TaintedEval Example of user-controlled input being passed into an eval call. ```php globals['$GLOBALS'])) { $config->globals['$GLOBALS'] = 'array{data: array}'; } ``` -------------------------------- ### RedundantConditionGivenDocblockType Example Source: https://psalm.dev/docs/running_psalm/issues/RedundantConditionGivenDocblockType Example showing a redundant condition given docblock type. ```php ``` -------------------------------- ### PossiblyNullFunctionCall Example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyNullFunctionCall Example of a function call on a possibly null value. ```php getTypes() as $atomic) handleAtomic($atomic); else handleAtomic($type); // with union container it becomes foreach ($types->getTypes() as $atomic) handleAtomic($atomic); ``` -------------------------------- ### PossiblyInvalidCast Example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyInvalidCast An example demonstrating a possibly invalid cast in PHP. ```php example Source: https://psalm.dev/docs/annotating_code/type_syntax/utility_types This example demonstrates the usage of `properties-of` to convert an object into an array, including its non-static properties. ```php class A { public string $foo = 'foo!'; public int $bar = 42; /** * @return properties-of */ public function asArray(): array { return [ 'foo' => $this->foo, 'bar' => $this->bar, ]; } /** * @return list>> */ public function attributeNames(): array { return ['foo', 'bar'] } } ``` -------------------------------- ### PossiblyInvalidOperand Example Source: https://psalm.dev/docs/running_psalm/issues/PossiblyInvalidOperand Example of using a possibly invalid value in an operation. ```php i++; } } /** * @psalm-immutable */ final class NotReallyImmutableClass extends MutableParent {} ```