nvim/test-diagnostics.php

71 lines
1.7 KiB
PHP

<?php
/**
* Test file for PHP LSP diagnostics
* Tests error, warning, info, and hint virtual text colors
*
* Note: Configure Intelephense to show different diagnostic levels:
* - diagnostics.undefinedSymbols = true (errors)
* - diagnostics.undefinedVariables = true (warnings)
* - diagnostics.unusedSymbols = "hint" (hints)
*/
// Define some valid functions and variables first to avoid cascading errors
function validFunction(): void {
echo "This is valid\n";
}
$definedVariable = 'I exist';
// ERROR: Undefined function
someUndefinedFunction();
// ERROR: Undefined variable
echo $completelyUndefined;
// WARNING/HINT: Unused variable (depends on Intelephense config)
$unused = 'Never used anywhere';
// Valid code to separate errors
validFunction();
// ERROR: Wrong number of arguments
function requiresTwoArgs( string $a, string $b ): void {
echo "$a $b\n";
}
requiresTwoArgs( 'only one' ); // Missing second argument
// ERROR: Type mismatch
function expectsString( string $param ): void {
echo $param;
}
expectsString( 123 ); // Passing int to string parameter
// WARNING: Variable might not be defined in all code paths
if ( rand( 0, 1 ) ) {
$maybeUndefined = 'sometimes defined';
}
// Commenting out to reduce errors: echo $maybeUndefined;
// Valid usage
echo $definedVariable . "\n";
// ERROR: Calling method on non-object
$notAnObject = 'string';
$notAnObject->someMethod();
// ERROR: Invalid array access (multiple levels)
$emptyArray = array();
echo $emptyArray['key']['nested']['deep'];
// Valid function call
requiresTwoArgs( 'hello', 'world' );
// HINT/WARNING: Unreachable code (some configs)
function hasDeadCode(): string {
return 'done';
echo 'This is unreachable'; // Dead code.
}
echo "Test complete!\n";