Skip to content

Commit

Permalink
Deprecate the Numeric class, ref #30 (#34)
Browse files Browse the repository at this point in the history
* Deprecate the Numeric class, ref #30

* Remove useless BC layer and use real DEPRECATED error code
  • Loading branch information
damienalexandre authored Apr 2, 2017
1 parent 762b773 commit f04ed96
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
CHANGELOG
=========

### Unreleased ###
### 1.0.2 (2017-03-31) ###

* fix PHP CS Fixer configuration and version
* fix #30 deprecate the PHP 7 reserved word Numeric class in favor of Unit

### 1.0.1 (2015-12-13) ###

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ Trademark
Handle trade­mark symbol ``, a reg­is­tered trade­mark symbol `®`, and a copy­right symbol `©`. This fixer replace
commonly used approximations: `(r)`, `(c)` and `(TM)`. A non-breaking space is put between numbers and copyright symbol too.

Numeric
Unit (formerly Numeric)
---------

Add a non-breaking space between a numeric and it's unit. Like this: `12_h`, `42_฿` or `88_%`.
Add a non-breaking space between a numeric and it's unit. Like this: `12_h`, `42_฿` or `88_%`. It was named `Numeric` before release 1.0.2, but BC is kept for now.

**It is really easy to make your own Fixers, feel free to extend the provided ones if they do not fit your typographic rules.**

Expand All @@ -159,7 +159,7 @@ en_GB
-----

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('en_GB');
```

Expand All @@ -169,7 +169,7 @@ fr_FR
Those rules apply most of the recommendations of "Abrégé du code typographique à l'usage de la presse", ISBN: 9782351130667.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_FR');
```

Expand All @@ -179,7 +179,7 @@ fr_CA
Mostly the same as fr_FR, but the space before punctuation points is not mandatory.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_CA');
```

Expand All @@ -189,7 +189,7 @@ de_DE
Mostly the same as en_GB, according to [Typefacts](http://typefacts.com/) and [Wikipedia](http://de.wikipedia.org/wiki/Typografie_f%C3%BCr_digitale_Texte).

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('de_DE');
```

Expand Down
17 changes: 6 additions & 11 deletions src/JoliTypo/Fixer/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@

namespace JoliTypo\Fixer;

use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;

/**
* Add nbsp between numeric and units.
* {@inheritdoc}
*
* @deprecated Numeric should not be used (reserved keyword in PHP7)
*/
class Numeric implements FixerInterface
class Numeric extends Unit
{
public function fix($content, StateBag $stateBag = null)
public function __construct()
{
// Support a wide range of currencies
$content = preg_replace('@([\dº])('.Fixer::ALL_SPACES.')+([º°%Ω฿₵¢₡$₫֏€ƒ₲₴₭£₤₺₦₨₱៛₹$₪৳₸₮₩¥\w]{1})@', '$1'.Fixer::NO_BREAK_SPACE.'$3', $content);

return $content;
@trigger_error('Numeric fixer is deprecated, use Unit instead. To be removed in 2.0.', E_USER_DEPRECATED);
}
}
28 changes: 28 additions & 0 deletions src/JoliTypo/Fixer/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace JoliTypo\Fixer;

use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;

/**
* Add nbsp between numeric and units.
*/
class Unit implements FixerInterface
{
public function fix($content, StateBag $stateBag = null)
{
// Support a wide range of currencies
$content = preg_replace('@([\dº])('.Fixer::ALL_SPACES.')+([º°%Ω฿₵¢₡$₫֏€ƒ₲₴₭£₤₺₦₨₱៛₹$₪৳₸₮₩¥\w]{1})@', '$1'.Fixer::NO_BREAK_SPACE.'$3', $content);

return $content;
}
}
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/EnglishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class EnglishTest extends \PHPUnit_Framework_TestCase
{
private $en_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');
private $en_fixers = array('Unit', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<!-- From https://en.wikipedia.org/wiki/Gif#Pronunciation -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

use JoliTypo\Fixer;

class NumericTest extends \PHPUnit_Framework_TestCase
class UnitTest extends \PHPUnit_Framework_TestCase
{
public function testNumericUnits()
{
$fixer = new Fixer\Numeric();
$this->assertInstanceOf('JoliTypo\Fixer\Numeric', $fixer);
$fixer = new Fixer\Unit();
$this->assertInstanceOf('JoliTypo\Fixer\Unit', $fixer);

$this->assertEquals('Test', $fixer->fix('Test'));
$this->assertEquals('1'.Fixer::NO_BREAK_SPACE.'h', $fixer->fix('1 h'));
Expand Down
2 changes: 1 addition & 1 deletion tests/JoliTypo/Tests/FrenchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class FrenchTest extends \PHPUnit_Framework_TestCase
{
private $fr_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');
private $fr_fixers = array('Unit', 'Ellipsis', 'Dimension', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<p>Ceci est à remplacer par une fâble :p</p>
Expand Down
8 changes: 8 additions & 0 deletions tests/JoliTypo/Tests/JoliTypoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ public function testNonHTMLContent()
$this->assertEquals(html_entity_decode($fixed, null, 'UTF-8'), $fixer->fixString($toFix));
$this->assertEquals('Here is a “protip©”!', $fixer->fixString('Here is a "protip(c)"!'));
}

public function testDeprecatedFixer()
{
$fixer = new Fixer(array('Numeric'));
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);

$this->assertEquals('3'.Fixer::NO_BREAK_SPACE.'', $fixer->fixString('3 €'));
}
}

class FakeFixer
Expand Down

0 comments on commit f04ed96

Please sign in to comment.