Git Product home page Git Product logo

php-camel-caser's Introduction

๐Ÿช๐Ÿ’ผ PHP Camel Caser

Build Status Coverage Status StyleCI Packagist

This package lets you use built-in PHP functions in camel case.

Installation

PHP Camel Caser can be easily installed using Composer. Just run the following command from the root of your project.

composer require divineomega/php-camel-caser

If you have never used the Composer dependency manager before, head to the Composer website for more information on how to get started.

Usage

After installing PHP Camel Caser, the new functions are available straight away.

Some example usage is shown below.

require_once __DIR__.'/vendor/autoload.php';

strReplace('c', 'b', 'cat');                // bat
strWordCount("Hello world!");               // 2
inArray('Picard', ['Picard', 'Janeway']);   // true

// and so on...

php-camel-caser's People

Contributors

divineomega avatar melbahja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

php-camel-caser's Issues

Aliased functions cannot be properly debugged, because of the use of eval

As user phordijk on Reddit explains:

Even when implementing this functions eval()ing code like that to create the function will totally fuck up your error messages. Good luck debugging your code.

Language aside, this person has a great point about being able to read back the correct information when debugging code.

To address this, I wrote a proof of concept that loads the aliases from file:

$code = array_reduce(
    get_defined_functions()['internal'] ?? [],
    function (string $carry, string $original): string {
        $alias = lcfirst(
            str_replace(
                ' ',
                '',
                ucwords(
                    str_replace(
                        ['-', '_'],
                        ' ',
                        $original
                    )
                )
            )
        );
        
        if ($alias != $original
            && !empty($alias)
            && !function_exists($alias)
        ) {
            // @todo Use reflection on the original to inherit
            // parameter list.
            $carry .= sprintf(
                <<<'FUNCTION'
if (!function_exists('%1$s')) {
    /**
     * @see %2$s
     */
    function %1$s (...$arguments) {
        return %2$s(...$arguments);
    }
}


FUNCTION
,
                $alias,
                $original
            );
        }
        
        return $carry;
    },
    '<?php' . PHP_EOL
);

// This should be solved with some sort of caching mechanism, to prevent
// having to do this every time.
$storage = tempnam(sys_get_temp_dir(), 'camelCasedInternalFunctions');

file_put_contents($storage, $code);
require $storage;

The code above solves the following:

  • No dependency on illuminate/support
  • No use of the eval function
    • No runtime evaluation of code
    • Ability to identify aliased functions in stack traces

About the cache: one could invalidate it by making a hash out of the available internal functions, like $hash = md5(implode(':', get_defined_functions()['internal'] ?? [])); and then to incorporate this in the file name of the cache file. That way, if a user installs or removes a PHP extension or library, the cache is invalidated automatically.

<?php
$storage = sprintf(
    __DIR__ . '/aliases.%s.php',
    md5(
        implode(
            ':',
            get_defined_functions()['internal'] ?? []
        )
    )
);

If you agree with the points made in this issue and would like help implementing this, I'm happy to help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.