# Review the Prefixed PHP Project

The results of the prefixing process of the project can be found in this repository: https://github.com/PHP-Prefixer/Getting-Started_prefixed (opens new window)

# The New Composer.json of the Prefixed Project

The PHP-Prefixed process has produced a new composer.json schema, contains all the original files and files that are re-organized according to the applied PPP prefix.

{
    "name": "php-prefixer/getting-started",
    "autoload": {
        "classmap": [
            "src/ASimpleLogWarning.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Formatter/FormatterInterface.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Formatter/JsonFormatter.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Formatter/LineFormatter.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Formatter/WildfireFormatter.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/AbstractHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/BufferHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/FingersCrossedHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/FirePHPHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/GroupHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/HandlerInterface.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/MailHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/NativeMailerHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/NullHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/StreamHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/SwiftMailerHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/SyslogHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Handler/TestHandler.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Logger.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Processor/IntrospectionProcessor.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Processor/MemoryPeakUsageProcessor.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Processor/MemoryProcessor.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Processor/MemoryUsageProcessor.php",
            "vendor_prefixed/monolog/monolog/src/Monolog/Processor/WebProcessor.php"
        ],
        "files": []
    }
}

The files of the Monolog library are stored in the vendor_prefixed folder. The composer.json declares them in the classmap attribute to generate the autoloader.

# The Prefixed Test

The project test file a-simple-test.php is mostly unchanged. The prefixer added a notice that it was processed:

<?php /* This file has been prefixed by <PHP-Prefixer> for "PHP-Prefixer Getting Started" */

# File: a-simple-test.php

use Acme\ASimpleLogWarning;

require __DIR__ . '/vendor/autoload.php';

$aSimpleLogWarning = new ASimpleLogWarning();
$aSimpleLogWarning->logWarn();

# The Prefixed Class

This is the class ASimpleLogWarning that uses the Monolog/Logger src/ASimpleLogWarning.php. Now it is prefixed like this:

<?php
/* This file has been prefixed by <PHP-Prefixer> for "PHP-Prefixer Getting Started" */

# File: src/ASimpleLogWarning.php

namespace Acme;

class ASimpleLogWarning
{
    public function logWarn()
    {
        $log = new \PPP\Monolog\Logger('name');
        $log->pushHandler(new \PPP\Monolog\Handler\StreamHandler('app.log', \PPP\Monolog\Logger::WARNING));
        $log->addWarning('Foo');
    }
}

The original \Monolog\Logger has been replaced with \PPP\Monolog\Logger; the rest of the references in the same file and all the declarations in the vendor_prefixed/monolog/monolog library folder have also been replaced.

When the test file is executed: php a-simple-test.php, it adds a new line to the log file app.log with the following sample output:

[2020-10-08 17:40:19] name.WARNING: Foo [] []
[2020-10-08 18:14:43] name.WARNING: Foo [] []

# Testing the Prefixed Monolog

Since the Monolog library has been fully prefixed, the original unit tests can be executed inside the vendor_prefixed folder to confirm that the library is correctly prefixed and is functionally equivalent to the original library.

~/vendor_prefixed/monolog/monolog$ phpunit
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

................................................................. 65 / 97 ( 67%)
................................                                  97 / 97 (100%)

Time: 116 ms, Memory: 6.00MB

A few advanced notes for future testers:

  • Monolog v1 uses a custom autoloader in the unit tests. It was manually fixed to load from the right directory. For instance: vendor_prefixed/monolog/monolog/tests/Monolog/Functional/Handler/FirePHPHandlerTest.php.
  • There is a test that checks the namespace of the loaded object. It has been updated to check the namespace prefixed with PPP. This is the modified test function vendor_prefixed/monolog/monolog/tests/Monolog/Formatter/LineFormatterTest.php / testDefFormatWithObject.

# Conclusion

In this guide, a simple project that uses Monolog (opens new window) Logger has been presented to show how the prefixer can be used. The original project writes a line in a log file, the same functionality is retained after the prefixing process, and the Monolog unit tests are passed.

Please, continue to the next guide to learn more about advanced cases.

Last Updated: 12/19/2023, 9:07:55 AM