2017 © Pedro Peláez
 

library is-deprecated

Check PHP user and core function is deprecated

image

samsonasik/is-deprecated

Check PHP user and core function is deprecated

  • Saturday, January 20, 2018
  • by samsonasik
  • Repository
  • 1 Watchers
  • 2 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

IsDeprecated

Latest Version Build Status Coverage Status PHPStan Downloads, (*1)

Introduction

IsDeprecated is PHP7 Helper that can help you detect if your function is deprecated with E_USER_DEPRECATED and E_DEPRECATED level., (*2)

Features

  • [x] Detect on independent function (E_USER_DEPRECATED)
  • [x] Detect on function inside class (E_USER_DEPRECATED)
  • [x] Detect on core php function (E_DEPRECATED)

Installation

Require uses composer:, (*3)

composer require samsonasik/is-deprecated

Usage

There are 2 functions:, (*4)

1. For user defined function, (*5)

/**
 * @param  string|array $function the "functionName" or ["ClassName" or object, "functionName"] or "ClassName::functionName"
 * @throws InvalidArgumentException when trigger_error found but the error is not E_USER_DEPRECATED
 * @throws InvalidArgumentException when trigger_error and E_USER_DEPRECATED found but misplaced
 * @return bool
 */
function isDeprecatedUser($function): bool

Note: when trigger_error E_USER_DEPRECATED inside condition, you need to use actual call with signature:, (*6)

/**
 * @param  callable $function callable function
 * @return bool
 */
function isDeprecatedWithActualCall(callable $function)

2. For core PHP function, (*7)

/**
 * @param  callable $function callable function
 * @return bool
 */
function isDeprecatedCore(callable $function): bool

Example On independent function, (*8)

include 'vendor/autoload.php'; // autoload may already handled by your framework

use function IsDeprecated\isDeprecatedUser;
use function IsDeprecated\isDeprecatedWithActualCall;

function foo()
{
    trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
    echo 'foo' . PHP_EOL;
}

function foonotdeprecated()
{
    echo 'foo' . PHP_EOL;
}

function fooDeprecatedWithCondition()
{
    if (1 === 1) {
        trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        echo 'foo' . PHP_EOL;
    }
}

// deprecated
var_dump(isDeprecatedUser('foo')); // true  OR for deprecate with condition
var_dump(isDeprecatedWithActualCall(function () {
    fooDeprecatedWithCondition();
})); // true

// not deprecated
var_dump(isDeprecatedUser('foonotdeprecated')); // false

// Usage Example:
if (isDeprecatedUser('foo')) {
    foonotdeprecated();;
} else {
    foo();
}

Example On function inside class, (*9)

include 'vendor/autoload.php'; // autoload may already handled by your framework

use function IsDeprecated\isDeprecatedUser;
use function IsDeprecated\isDeprecatedWithActualCall;

class Aclass
{
    function foo()
    {
        trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        echo 'foo' . PHP_EOL;
    }

    function foodeprecatedWithCondition()
    {
        if (1 === 1) {
            trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        }
        echo 'foo' . PHP_EOL;
    }

    function foonotdeprecated()
    {
        echo 'foo' . PHP_EOL;
    }

    // you may call inside the class
    // with $this as first index of array parameter
    function execute()
    {
        if (isDeprecatedUser([$this, 'foo'])) {
            $this->foonotdeprecated();
            return;
        }

        $this->foo();
    }
}

// deprecated
var_dump(isDeprecatedUser(['Aclass', 'foo'])); // true OR
var_dump(isDeprecatedUser([new \Aclass(), 'foo'])); // true OR
var_dump(isDeprecatedUser('Aclass::foo')); // true OR for deprecate with condition

var_dump(isDeprecatedWithActualCall(function () { // true
    new \Aclass()->foo();
}));

// not deprecated
var_dump(isDeprecatedUser(['Aclass', 'foonotdeprecated'])); // false OR
var_dump(isDeprecatedUser([new \Aclass, 'foonotdeprecated'])); // false OR
var_dump(isDeprecatedUser('Aclass::foonotdeprecated')); // false

// Usage Example:
if (isDeprecatedUser(['Aclass', 'foo'])) {
    (new \Aclass())->foonotdeprecated();;
} else {
    (new \Aclass())->foo();;
}

Example On core PHP function, (*10)

include 'vendor/autoload.php'; // autoload may already handled by your framework

use function IsDeprecated\isDeprecatedCore;

$function = function () {
    mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
};

//on php 7.1
var_dump(isDeprecatedCore($function)); // true

//on php 7.0
var_dump(isDeprecatedCore($function)); // false

// Usage Example:
if (isDeprecatedCore($function)) {
    // alternative function, eg: openssl ...
} else {
    $function();
}

Limitation

For Core PHP Functions or user function with condition (T_IF or T_SWITCH token), the function passed actually need to be called. It ensure that we don't get error during call deprecated function, and we can use alternative function if the isDeprecatedCore() returns true with call of isDeprecatedWithActualCall., (*11)

Contributing

Contributions are very welcome. Please read CONTRIBUTING.md, (*12)

The Versions

20/01 2018

dev-master

9999999-dev https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

20/01 2018

2.0.7

2.0.7.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

20/01 2018

2.0.6

2.0.6.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

19/01 2018

2.0.5

2.0.5.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

19/01 2018

2.0.4

2.0.4.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

19/01 2018

2.0.3

2.0.3.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

18/01 2018

2.0.2

2.0.2.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

18/01 2018

2.0.1

2.0.1.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

18/01 2018

2.0.0

2.0.0.0 https://github.com/samsonasik/IsDeprecated

Check PHP user and core function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated

04/02 2017

1.0.0

1.0.0.0 https://github.com/samsonasik/IsDeprecated

Check PHP user function is deprecated

  Sources   Download

MIT

The Requires

 

The Development Requires

php check deprecated