PHP tools and completion exercises
PHP formatting and completion
Two independent plugins are available in Extensions:
- Format PHP formats the current PHP buffer in a worker, with cursor preservation and one-step Undo. It does not write to disk. Invalid PHP, tool failure, a 30-second timeout, or a changed/switched document leaves the active text intact.
- PHP Format Settings selects line width and target PHP version. Editor tab width supplies the default indentation; project Prettier and EditorConfig settings override defaults.
- PHP Completion (Ctrl+Space) requests suggestions. Completion also appears automatically while typing PHP identifiers,
->, or::. Use arrows to select, Enter/Tab or a click to insert, and Escape to dismiss. Other file types retain their normal editing keys. - PHP Completion Settings controls automatic suggestions, typing delay (default 120 ms), minimum characters, result limit, project PHP version and extra library/framework-stub folders. Settings are validated and saved atomically per editor/project.
- PHP Completion Help shows usage, workspace, worker phase, response timing and the last error. Reindex PHP Project rebuilds the workspace after external file changes.
Completion uses Intelephense's free completion features through LSP, not a regex symbol guesser. It resolves namespaces, class imports, aliases, grouped use, imported functions, Composer PSR-4 classes and included files present in the workspace. Standard LSP additional edits support inserted use declarations. The workspace comes from a containing Explorer project, then a Composer/git ancestor, then the file folder. Saved PHP files notify the server; Explorer filesystem operations trigger reindexing on the next request. The first request may take several seconds to index. PHP buffers above 2 MB are excluded. Numeric snippets, default parameter placeholders and choices are supported; Tab/Shift+Tab navigates parameters. Mirrored placeholders, snippet variables and transforms are rejected safely instead of modifying the document incorrectly.
The plugins activate only for PHP-family extensions, including .php and .phtml. No PHP interpreter or Composer executable is needed for formatting/completion. Executing PHP still requires PHP; no PHP runtime was available during this audit. Dependencies missing from a PHP application's vendor folder must be installed separately by that application's developer.
The tool packages are pinned in tools/php/package.json and package-lock.json. They are already installed on this development computer. To restore them after copying the editor to another computer, install Node.js and npm, then run from the editor directory:
npm ci --prefix tools/php --ignore-scriptsNode.js 22+ is recommended because some transitive dependencies require it. The tested PHP features passed here with the existing Node.js 20.19.2, which produced engine warnings for those dependencies during installation. No system packages were installed. PHP dependencies are optional, occupy about 172 MB locally, and are separate from the lightweight editor core. node_modules is excluded from source control. Intelephense is proprietary software with free completion features; this integration does not use premium features or supply a licence key. Keep its licence with any permitted local installation.
Sources: Prettier PHP, Prettier API, Intelephense documentation and licence information, Language Server Protocol.
PHP test document
python3 -B pocket_editor.py examples/php/demo.phpSee examples/php/README.md for completion exercises. Run both suites on a graphical desktop:
python3 -B -m unittest -v test_editor test_phpThe latest combined suite passed 29 tests on Debian. Windows/macOS UI, process handling, and tool installation still require platform verification.
Refined completion behavior
The PHP worker cancels obsolete requests, keeps only the latest waiting task, warms analysis after opening PHP files, and maintains a short-lived bounded exact-result cache. The UI checks document revision and cursor identity rather than copying the full document repeatedly while polling. Suggestions are ranked using prefix/case and LSP ordering. The popup provides scrolling, symbol kind/type, lazily resolved documentation when supported, response timing, and keyboard/page navigation. Escape cancels delayed/in-flight results as well as hiding the popup, so an older request cannot reopen it.
Project indexes live under tools/php/cache/, excluded from source control. Concurrent sessions use a temporary workspace if the persistent cache is already locked. POSIX stale PID locks are recovered; read-only cache locations also fall back to temporary storage. No user source files are written by completion. Existing document font and toolbar shortcut preferences are preserved.
For your PHP code: open its folder with Extensions → Open Project, open a PHP document, then use PHP Completion Settings → Add folder… for libraries outside the project or framework stubs. Select the project's PHP version there. Composer dependencies must already be present. use, aliases, inheritance, traits, interfaces, enums, typed return values, PHPDoc array element types, PHPDoc magic methods, built-in functions and external library paths are covered by real-server tests. Highly dynamic eval, runtime-generated members, magic without annotations and framework-specific templates are not guaranteed; provide PHPDoc/stubs where needed. This is not a claim of complete support for every PHP program or framework.
examples/php/advanced_completion.php adds advanced exercises. audit/php-completion-performance.json records the measured demo timings: first worker request 1,178.42 ms (a persisted index may be reused), five warm requests median 14.72 ms (range 4.74–155.55 ms), and ten exact-cache repeats median 0.17 ms. These worker measurements exclude the configurable typing debounce and Tk rendering and are not a performance guarantee for large projects. Only the bundled PHP fixtures and temporary external-library projects were available for validation; no separate user application was supplied.
PHP example project
Open demo.php with Pocket Editor. Extensions → Format PHP formats the buffer when needed, and Edit → Undo restores a formatting change. It may already be formatted if you previously saved it. Save only when you want to update the sample file.
For completion exercises, add a new PHP line after the existing code (not inside a comment), type a prefix, then press Ctrl+Space:
| Prefix | Expected completion | Source |
|---|---|---|
$customer->disp | displayName | use PocketDemo\Models\Customer, defined in src/Models/Customer.php |
$greeter->gre | greetCustomer | WelcomeService alias for src/Services/Greeter.php |
WelcomeService::defaultG | defaultGreeting | Static method on the imported alias |
format_customer_l | format_customer_label | use function from includes/helpers.php |
The first request indexes this small Composer project. Select with arrows and insert with Enter/Tab, or click a suggestion. Escape closes the popup. Function/method completions can insert parentheses and parameter placeholders. Tab/Shift+Tab moves between parameters; Escape ends placeholder navigation.
grouped.php tests grouped class imports. includes/legacy.php supplies a global helper for require/include completion tests. composer.json supplies the PSR-4 namespace map without requiring Composer installation. The valid sample program uses explicit require_once statements for execution; running it requires a separate PHP interpreter.
To see changed-file tracking, edit and save an imported class, then request completion in demo.php. Use Extensions → Reindex PHP Project after filesystem changes made outside the editor.
Your PHP application
Use Extensions → Open Project to select your application's folder. Open its PHP document and choose Extensions → PHP Completion Settings. Set the target PHP version and add extra source/library or framework-stub folders that live outside the project. Normal project files, Composer namespaces and installed vendor dependencies are discovered by the language server. No PHP application code is executed by completion.
Advanced exercises
Open advanced_completion.php. Its comments show inheritance, traits, interface implementations, enum cases, PHPDoc magic methods, typed return chaining and built-in function examples. The actual declarations are in src/Advanced/UserRepository.php. Suggestions can be requested on a new PHP line with Ctrl+Space. To test nullable members inside a typed function, use function example(?Customer $customer) { $customer?->disp and request completion at the end (incomplete code is allowed while editing, but must be fixed before formatting/executing).
The popup shows symbol type and available documentation, with scrolling and Page Up/Page Down. Default automatic delay is 120 ms; tune it or disable automatic suggestions in PHP Completion Settings. Completion runs only for PHP-family files, preserving normal keys in other documents. Dynamic framework methods require useful type declarations, PHPDoc or stubs; not every runtime-generated API can be inferred.