PHP code example of szepeviktor / consistent-versions

1. Go to this page and download the library: Download szepeviktor/consistent-versions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

szepeviktor / consistent-versions example snippets



/**
 * Plugin Name: Example Plugin
 * Version: 1.2.3
 * Requires PHP: 8.1
 */


/**
 * Plugin Name: Example Plugin
 * Version: 1.2.3
 * Requires at least: 6.4
 * Requires PHP: 8.1
 */



declare(strict_types=1);

namespace Vendor;

final class Plugin
{
    public const VERSION = '1.2.3';
}



namespace Vendor;

const PACKAGE_VERSION = '1.2.3';

final class Plugin
{
    public const VERSION = '1.' . '2.3';
}



declare(strict_types=1);

use SzepeViktor\ConsistentVersions\ConfigurationLoader;
use SzepeViktor\ConsistentVersions\Engine;

nfiguration, __DIR__);

foreach ($report->results as $result) {
    if ($result->passed()) {
        continue;
    }

    echo $result->name, ': expected ', (string) $result->expected, PHP_EOL;

    foreach ($result->mismatches as $label => $actual) {
        echo '  ', $label, ': ', (string) $actual, PHP_EOL;
    }
}

exit($report->passed() ? 0 : 1);

$report->passed();
$report->failureCount();

$result->name;
$result->expected;
$result->mismatches;
$result->passed();



declare(strict_types=1);

namespace Project;

use SzepeViktor\ConsistentVersions\Document;
use SzepeViktor\ConsistentVersions\Reader\Reader;

final class ManifestReader implements Reader
{
    public function read(string $path): Document
    {
        $contents = file_get_contents($path);

        if ($contents === false) {
            throw new \RuntimeException(sprintf('Could not read %s', $path));
        }

        return new Document(
            ['release' => trim($contents)],
            $path
        );
    }
}

use Project\ManifestReader;
use SzepeViktor\ConsistentVersions\Engine;
use SzepeViktor\ConsistentVersions\Normalizer\NormalizerRegistry;
use SzepeViktor\ConsistentVersions\Reader\ReaderRegistry;

$readers = ReaderRegistry::withDefaults();
$readers->register('manifest', new ManifestReader());

$engine = new Engine(
    $readers,
    NormalizerRegistry::withDefaults()
);



declare(strict_types=1);

namespace Project;

use SzepeViktor\ConsistentVersions\Normalizer\Normalizer;

final class LowercaseNormalizer implements Normalizer
{
    public function normalize(
        string|int|float|bool|null $value
    ): string|int|float|bool|null {
        return is_string($value) ? strtolower($value) : $value;
    }
}

use Project\LowercaseNormalizer;
use SzepeViktor\ConsistentVersions\Engine;
use SzepeViktor\ConsistentVersions\Normalizer\NormalizerRegistry;
use SzepeViktor\ConsistentVersions\Reader\ReaderRegistry;

$normalizers = NormalizerRegistry::withDefaults();
$normalizers->register('lowercase', new LowercaseNormalizer());

$engine = new Engine(
    ReaderRegistry::withDefaults(),
    $normalizers
);
text
VERSION
plugin.php
readme.txt
composer.json
src/Plugin.php
text
composer.json
plugin.php
readme.txt
.github/workflows/tests.yml
phpcs.xml
json
{
    "Plugin Name": "Example Plugin",
    "Version": "1.2.3",
    "Requires PHP": "8.1"
}
yaml
checks:
  plugin-version:
    normalize: version

    expected:
      reader: text
      file: VERSION

    values:
      plugin-header:
        reader: wordpress-plugin
        file: plugin.php
        path: $.Version

      wordpress-readme:
        reader: wordpress-readme
        file: readme.txt
        path: $['Stable tag']

      php-constant:
        reader: php
        file: src/Plugin.php
        path: $.classes['Vendor\\Plugin'].constants.VERSION
yaml
normalize:
  - trim
  - trim-v-prefix
  - version
ini
PHP_VERSION="8.1"
PLUGIN_VERSION="1.2.3"

[tool]
version="2.0.0"
yaml
jobs:
  tests:
    strategy:
      matrix:
        php:
          - "8.1"
          - "8.2"
xml
<project active="true">
    <version channel="stable">1.2.3</version>
</project>
markdown
---
version: 1.2.3
yaml
path: $.frontMatter.
yaml
checks:
  release-version:
    normalize:
      - trim-v-prefix
      - version

    expected:
      reader: text
      file: VERSION

    values:
      composer:
        reader: composer
        file: composer.json
        path: $.version

      plugin-header:
        reader: wordpress-plugin
        file: plugin.php
        path: $.Version

      wordpress-readme:
        reader: wordpress-readme
        file: readme.txt
        path: $['Stable tag']

      php-constant:
        reader: php
        file: src/Plugin.php
        path: $.classes['Vendor\\Plugin'].constants.VERSION

  minimum-php:
    normalize: version

    expected:
      reader: composer
      file: composer.json
      path: $.
text
ERROR: Configuration file is not readable: consistent-versions.yaml