Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the Numeric class, ref #30 #34

Merged
merged 2 commits into from
Apr 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
5 changes: 5 additions & 0 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ private function compileRules($rules)
$fixer = $rule;
$className = get_class($rule);
} else {
/* BC Layer: the Numeric class is deprecated */
if (mb_strtolower($rule) === 'numeric') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this layer is not necessary as the old class still exists

$rule = 'Unit';
}

$className = class_exists($rule) ? $rule : (class_exists('JoliTypo\\Fixer\\'.$rule) ? 'JoliTypo\\Fixer\\'.$rule : false);
if (!$className) {
throw new BadRuleSetException(sprintf('Fixer %s not found', $rule));
Expand Down
15 changes: 6 additions & 9 deletions src/JoliTypo/Fixer/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@
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.', E_USER_NOTICE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably add a @ silencer and use a deprecation error level:
@trigger_error('...', 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