Skip to content

Commit

Permalink
Fixed version truncation so one client doesn't affect the others (mat…
Browse files Browse the repository at this point in the history
…omo-org#6075)

When `ParserAbstract::$maxMinorParts` is changed by any
Client Parser, it is propagated to all Client Parsers. This
happens because static properties defined ONLY in the parent
class shares a common value with the child class.

To set version truncation specifically for a custom ClientParser, we
need to redefine `ParserAbstract::$maxMinorParts` in the child class
and use late static binding to access the property.

Example:
```
<?php

use DeviceDetector\Parser\Client\ClientParserAbstract;

class MyAppParser extends ClientParserAbstract{
    protected static $maxMinorParts = self::VERSION_TRUNCATION_PATCH;
    protected $fixtureFile = 'my_app.yml';
    protected $parserName = 'myapp';

    protected function getRegexesDirectory(){
        return __DIR__;
    }
}
````

More on late static binding in PHP:
https://www.php.net/manual/en/language.oop5.late-static-bindings.php

Co-authored-by: Emmanuel Ogbodo <[email protected]>
Co-authored-by: Stefan Giehl <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2020
1 parent c826614 commit 681cd10
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Parser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static function setVersionTruncation($type)
self::VERSION_TRUNCATION_MAJOR,
self::VERSION_TRUNCATION_MINOR,
self::VERSION_TRUNCATION_PATCH))) {
self::$maxMinorParts = $type;
static::$maxMinorParts = $type;
}
}

Expand Down Expand Up @@ -229,9 +229,9 @@ protected function buildVersion($versionString, $matches)
{
$versionString = $this->buildByMatch($versionString, $matches);
$versionString = str_replace('_', '.', $versionString);
if (null !== self::$maxMinorParts && substr_count($versionString, '.') > self::$maxMinorParts) {
if (null !== static::$maxMinorParts && substr_count($versionString, '.') > static::$maxMinorParts) {
$versionParts = explode('.', $versionString);
$versionParts = array_slice($versionParts, 0, 1+self::$maxMinorParts);
$versionParts = array_slice($versionParts, 0, 1+static::$maxMinorParts);
$versionString = implode('.', $versionParts);
}
return trim($versionString, ' .');
Expand Down

0 comments on commit 681cd10

Please sign in to comment.