Skip to main content

filestate_php

Version: 6.4.11

wexample-filestate-php adds PHP support to wexample-filestate: a PhpFile target type that enforces the .php extension, and a phpcs_fixer option that drives PHP-CS-Fixer inside a dedicated Docker container to rewrite source files to a consistent code style. It is aimed at Python developers who use the wexample-filestate declarative framework to manage and enforce the state of PHP codebases.

Table of Contents

Installation

pip install wexample-filestate-php

Requires Python >=3.10.

Quickstart

Install the package:

pip install wexample-filestate-php

Register PhpOptionsProvider when you create the manager, then set "class": PhpFile on any child that should be treated as a PHP file and "php": {"phpcs_fixer": True} to enforce code style:

from pathlib import Path

from wexample_filestate.options_provider.default_options_provider import DefaultOptionsProvider
from wexample_filestate.utils.file_state_manager import FileStateManager
from wexample_filestate_php.file.php_file import PhpFile
from wexample_filestate_php.options_provider.php_options_provider import PhpOptionsProvider
from wexample_prompt.common.io_manager import IoManager

manager = FileStateManager.create_from_path(
    io=IoManager(),
    path=Path("/path/to/project"),
    options_providers=[DefaultOptionsProvider, PhpOptionsProvider],
)

manager.configure(config={
    "children": [
        {
            "name": "index.php",
            "class": PhpFile,
            "php": {"phpcs_fixer": True},
        }
    ]
})

manager.apply()

After apply(), index.php is passed through PHP-CS-Fixer running inside a dedicated Docker container. If the file already conforms to the configured style the call is a no-op.

"php" also accepts a list shorthand — "php": ["phpcs_fixer"] is equivalent to the dict form above. PhpFile enforces the .php extension; omitting "class": PhpFile treats the entry as a plain ItemTargetFile without extension enforcement.

Tests

This project uses pytest for testing and pytest-cov for code coverage analysis.

Installation

First, install the required testing dependencies:

.venv/bin/python -m pip install pytest pytest-cov

Basic Usage

Run all tests with coverage:

.venv/bin/python -m pytest --cov --cov-report=html

Common Commands

# Run tests with coverage for a specific module
.venv/bin/python -m pytest --cov=your_module

# Show which lines are not covered
.venv/bin/python -m pytest --cov=your_module --cov-report=term-missing

# Generate an HTML coverage report
.venv/bin/python -m pytest --cov=your_module --cov-report=html

# Combine terminal and HTML reports
.venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html

# Run specific test file with coverage
.venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing

Viewing HTML Reports

After generating an HTML report, open htmlcov/index.html in your browser to view detailed line-by-line coverage information.

Coverage Threshold

To enforce a minimum coverage percentage:

.venv/bin/python -m pytest --cov=your_module --cov-fail-under=80

This will cause the test suite to fail if coverage drops below 80%.

Architecture

wexample-filestate-php is a thin extension layer on top of wexample-filestate. It adds one file type, one config option tree, and one Docker-backed operation. Everything lives under src/wexample_filestate_php.

Parts

File type — PhpFile

src/wexample_filestate_php/file/php_file.py extends ItemTargetFile with a single class variable:

EXTENSION_ENV: ClassVar[str] = "php"

and overrides _expected_file_name_extension to return it. That is the entirety of the type: it enforces that any target declared with "class": PhpFile carries the .php extension. The constant PHP_FILE_EXTENSION in src/wexample_filestate_php/const/php_file.py mirrors this value for code that needs it without importing the class.

Options provider — PhpOptionsProvider

src/wexample_filestate_php/options_provider/php_options_provider.py is the entry point callers pass to FileStateManager. It extends AbstractOptionsProvider and returns [PhpOption] from get_options(). It also exposes get_docker_image_name(), which delegates to AbstractPhpFileContentOption.DOCKER_IMAGE_NAME ("php-option"), so the framework knows which Docker image is associated with this provider.

Top-level option — PhpOption

src/wexample_filestate_php/option/php_option.py is the "php" key in a file's config dict. It extends AbstractNestedConfigOption and mixes in OptionMixin and WithBatchDockerOptionMixin. Two things happen here:

  • set_value converts the list shorthand (["phpcs_fixer"]) into a dict ({"phpcs_fixer": True}) before handing the value to the parent.
  • get_allowed_options returns [PhpcsFixerOption], which is the set of child options the framework will instantiate from the dict.
  • create_required_operation delegates to _create_child_required_operation, so the actual operation is produced by whichever child option matches the dict keys.

The accepted raw value type is Union[list[str], dict, PhpConfigValue], defined in src/wexample_filestate_php/config_value/php_config_value.py.

Config value — PhpConfigValue

src/wexample_filestate_php/config_value/php_config_value.py is a typed ConfigValue with one public field:

phpcs_fixer: bool | None = public_field(default=None, ...)

to_option_raw_value converts the structured value back to the dict form the option tree expects, keyed on PhpcsFixerConfigOption.get_name() ("phpcs_fixer").

Config option name — PhpcsFixerConfigOption

src/wexample_filestate_php/config_option/phpcs_fixer_config_option.py is a minimal AbstractConfigOption whose only job is to declare the string name "phpcs_fixer" used as the dict key.

Abstract base for Docker-backed options — AbstractPhpFileContentOption

src/wexample_filestate_php/option/php/abstract_php_file_content_option.py extends AbstractFileContentOption and WithBatchDockerOptionMixin. It sets the Docker image name to "php-option" and resolves the Dockerfile path at runtime:

package_root = current_file.parent.parent.parent
return package_root / "resources" / "docker" / "Dockerfile.php-option"

This makes the Dockerfile location a property of the class, not of the caller.

Concrete option — PhpcsFixerOption

src/wexample_filestate_php/option/php/phpcs_fixer_option.py is the only concrete child option. It overrides two methods from the batch-Docker mixin:

  • _apply_content_change — checks the in-memory batch cache for the target path and returns the cached content, or falls back to reading the file unchanged. This is called per-file after the batch run.
  • _run_batch_on_paths — the actual work. It resolves which PHP-CS-Fixer config to use (the project's .php-cs-fixer.dist.php if present, otherwise the bundled one at /home/appuser/.php-cs-fixer.dist.php), ensures the Docker container is running, rebases all host paths to container paths via the runner, and executes:
["php-cs-fixer", "fix", f"--config={config_path}", "--path-mode=override", "--using-cache=no", *container_paths]

Docker image

src/wexample_filestate_php/resources/docker/Dockerfile.php-option defines the container used by all PHP options. It starts from php:8.3-cli, installs Composer, then installs friendsofphp/php-cs-fixer globally as a non-root user (appuser, UID 1000). The bundled fallback config src/wexample_filestate_php/resources/docker/.php-cs-fixer.dist.php is copied into the image at /home/appuser/.php-cs-fixer.dist.php; it enforces PSR-12 plus a standard set of style rules. The container stays alive with CMD ["tail", "-f", "/dev/null"] so subsequent calls reuse it.

Call path

When manager.apply() is called on a file configured with "php": {"phpcs_fixer": True}:

  1. PhpOptionsProvider.get_options() has already made PhpOption available under the "php" key.
  2. PhpOption.set_value normalises the value and stores it; get_allowed_options returns [PhpcsFixerOption], so the framework instantiates one PhpcsFixerOption child.
  3. PhpOption.create_required_operation calls _create_child_required_operation, which asks PhpcsFixerOption to produce its operation.
  4. During apply, PhpcsFixerOption._run_batch_on_paths is called with the collected list of .php paths. It ensures the "php-option" container is running (building it from the Dockerfile if needed), translates host paths to container paths, and invokes php-cs-fixer fix inside the container.
  5. The runner populates the batch cache. Each file's _apply_content_change then reads from the cache to return the reformatted content, which the framework writes back to disk.

If the project root contains .php-cs-fixer.dist.php, that file is used as the fixer config (mounted at /var/www/html/.php-cs-fixer.dist.php); otherwise the bundled config inside the image is used.

Integration in the Suite

This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.

The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.

Visit the Wexample Suite documentation for the complete package ecosystem.

Dependencies

  • attrs: >=23.1.0
  • cattrs: >=23.1.0
  • wexample-filestate: >=17.3.0

Versioning & Compatibility Policy

Wexample packages follow Semantic Versioning (SemVer):

  • MAJOR: Breaking changes
  • MINOR: New features, backward compatible
  • PATCH: Bug fixes, backward compatible

We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Free to use in both personal and commercial projects.

About us

Wexample stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.

This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.

Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.

Known Limitations & Roadmap

Current limitations and planned features are tracked in the GitHub issues.

See the project roadmap for upcoming features and improvements.

Status & Compatibility

Maturity: Production-ready

Python Support: >=3.10

OS Support: Linux, macOS, Windows

Status: Actively maintained

Migration Notes

When upgrading between major versions, refer to the migration guides in the documentation.

Breaking changes are clearly documented with upgrade paths and examples.

Metadata

Release files for wexample-filestate-php 6.4.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wexample-filestate-php 6.4.11
File Size Uploaded
wexample_filestate_php-6.4.11.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wexample-filestate-php 6.4.11
File Interpreter ABI Platform
wexample_filestate_php-6.4.11-py3-none-any.whl Python 3 none any Details

Total release size: 27.7 kB

Release files / wexample_filestate_php-6.4.11.tar.gz

Download URL wexample_filestate_php-6.4.11.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8b6d4dd0ada28ea07f0774015961d270f329a152eae3c3fb2c1da1923abec214
BLAKE2b-256 checksum
How to use checksums
ff164da561f33adf8177937ce49287983234bff1710d7f71300282908def973d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.25.9 CPython/3.12.3 Linux/6.8.0-139-generic

Release files / wexample_filestate_php-6.4.11-py3-none-any.whl

Download URL wexample_filestate_php-6.4.11-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
260687027343e4bfd93bbd4d009cef95097cfd5f4fed9805fa33000171db7995
BLAKE2b-256 checksum
How to use checksums
9bbfc5591735fbf7056172501b4a20c6c0cc411408813fcd836266bb84f602ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.25.9 CPython/3.12.3 Linux/6.8.0-139-generic

Release history Release notifications | RSS feed

6.5.0

2 release files

This release

6.4.11 This release

2 release files

6.4.10

2 release files

6.4.9

2 release files

6.4.8

2 release files

6.4.7

2 release files

6.4.6

2 release files

6.4.5

2 release files

6.4.4

2 release files

6.4.3

2 release files

6.4.2

2 release files

6.4.1

2 release files

6.4.0

2 release files

6.3.8

2 release files

6.3.7

2 release files

6.3.6

2 release files

6.3.5

2 release files

6.3.4

2 release files

6.3.3

2 release files

6.3.2

2 release files

6.3.1

2 release files

6.3.0

2 release files

6.1.0

2 release files

6.0.30

2 release files

6.0.29

2 release files

6.0.28

2 release files

6.0.23

2 release files

6.0.21

2 release files

6.0.20

2 release files

0.1.0

2 release files

0.0.15

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.8

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page