A utility package that helps inspect functions in PHP.

Overview

A utility package that helps inspect functions in PHP.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides some utilities for inspecting functions (callables) in PHP. You can use it to find parameters, return types and more.

Support development

If you would like to support the on going maintenance and development of this package, please consider sponsoring me on GitHub.

Installation

You can install the package via Composer:

composer require ryangjchandler/fn-inspector

Usage

You can create a new instance of FnInspector by calling FnInspector::new() and passing a string, array or callable.

use RyanChandler\FnInspector\FnInspector;

$inspector = FnInspector::new('strlen');
$inspector = FnInspector::new(function () {});
$inspector = FnInspector::new([MyClass::class, 'staticMethod']);
$inspector = FnInspector::new([MyClass::class, 'instanceMethod']);

Return Type

You can retrieve the return type of a function by calling the returnType method.

$returnType = FnInspector::new(function (): void {})
    ->returnType()
    ->getName();

echo $returnType; // `void`

Number of Parameters

You can retrieve the number of parameters by calling the numberOfParameters method.

$params = FnInspector::new(function ($name): void {})
    ->numberOfParameters();

echo $params; // `1`

Number of Required Parameters

If you're only interested in required parameters, you can provide a bool to the numberOfParameters method.

$params = FnInspector::new(function ($name = null): void {})
    ->numberOfParameters(required: true);

echo $params; // `0`

Parameters

You can retrieve an instance of ParameterFinder by calling the parameters method. The ParameterFinder class provides a set of granular utilities to help you find parameters by name and type.

$parameters = FnInspector::new(function ($name = null): void {})
    ->parameters();

To get the first parameter in a function, use the first method.

$first = $parameters->first()->getName(); // returns `name`.

If you wish to find a parameter by type, you can use the type method.

// Returns a new instance of `ParameterFinder` with parameters matching the type provides.
$models = $parameters->type(Model::class);

You can find parameters that match multiple types by providing an array to the type method.

// Returns a new instance of `ParameterFinder` with parameters matching the types provides.
$usersAndCustomers = $parameters->type([User::class, Customer::class]);

You can retrieve all parameters by calling the all method.

// Returns an array of `ReflectionParameter` objects.
$usersAndCustomers = $parameters->all();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Warden is a CLI utility for orchestrating Docker based developer environments

Warden Warden is a CLI utility for orchestrating Docker based developer environments, and enables multiple local environments to run simultaneously wi

Glob-like file and pattern matching utility.

Glob-like file and pattern matching utility.

The Pantheon CLI — a standalone utility for performing operations on the Pantheon Platform

terminus : Pantheon's Command-Line Interface Status About Terminus is Pantheon's Command Line Interface (CLI), providing at least equivalent functiona

PHP package that provides functions for calculating mathematical statistics of numeric data
PHP package that provides functions for calculating mathematical statistics of numeric data

Statistics PHP package PHP package that provides functions for calculating mathematical statistics of numeric data. In this package I'm collecting som

PHP package that provides functions for calculating mathematical statistics of numeric data.
PHP package that provides functions for calculating mathematical statistics of numeric data.

Statistics PHP package PHP package that provides functions for calculating mathematical statistics of numeric data. In this package I'm collecting som

A package that provides `array_*` like functions for iterators.

The doekenorg/iterator-functions package provides a curated set of array_* like functions for iterators in PHP. This package is built to encourage developers to make more use of Iterators by simplifying common tasks.

This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

churn-php is a package that helps you identify php files in your project that could be good candidates for refactoring
churn-php is a package that helps you identify php files in your project that could be good candidates for refactoring

churn-php Helps discover good candidates for refactoring. Table of Contents What Is it? Compatibility How to Install? How to Use? How to Configure? Si

A Laravel package which helps you to flush sessions with an artisan command.

A simple laravel Package to flush sessions via artisan command. Sometimes we store data on sessions such as cart data or maybe any information regardi

Releases(v0.1.0)
Owner
Ryan Chandler
Ryan Chandler
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

Lodash PHP 474 Dec 31, 2022
Utility that helps you switch git configurations with ease.

git-profile Utility that helps you switch git configurations with ease Preface It is possible that you have multiple git configurations. For example:

Zeeshan Ahmad 240 Jul 18, 2022
Here is the top 100 PHP functions: it is the list of the most often used PHP native functions

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions. If you are a PHP developer, you must know the Top 100 PHP Functions deeply.

Max Base 16 Dec 11, 2022
Magento-Functions - A Resource of Magento Functions

Magento-Functions A Resource of Magento Functions Table of Contents Category Product User Cart Checkout General Account [Working w/ URL's] (#urls) Cat

Bryan Littlefield 28 Apr 19, 2021
Php-timer - Utility class for timing

phpunit/php-timer Utility class for timing things, factored out of PHPUnit into a stand-alone component. Installation You can add this library as a lo

Sebastian Bergmann 7.4k Jan 5, 2023
An utility component for XML usage and best practices in PHP

An utility component for XML usage and best practices in PHP

Laminas Project 13 Nov 26, 2022
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

CakePHP 112 Feb 15, 2022
Small utility library that handles metadata minification and expansion.

composer/metadata-minifier Small utility library that handles metadata minification and expansion.

Composer 134 Dec 26, 2022
m4b-tool is a command line utility to merge, split and chapterize audiobook files such as mp3, ogg, flac, m4a or m4b

m4b-tool m4b-tool is a is a wrapper for ffmpeg and mp4v2 to merge, split or and manipulate audiobook files with chapters. Although m4b-tool is designe

Andreas 798 Jan 8, 2023
Xr - Lightweight debug server utility built on ReactPHP.

XR ?? Subscribe to the newsletter to don't miss any update regarding Chevere. XR is a dump server utility built on top of ReactPHP. Features ✨ Dump si

Chevere 195 Dec 17, 2022