2017 © Pedro Peláez
 

project typesafe-functions

Type safe wrappers around internal functions that return mixed types

image

edmondscommerce/typesafe-functions

Type safe wrappers around internal functions that return mixed types

  • Thursday, July 5, 2018
  • by edmondscommerce
  • Repository
  • 1 Watchers
  • 0 Stars
  • 347 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Typesafe Functions

To assist with boilerplate around working with internal functions but ensuing that your code keeps tools like phpstan and phpqa happy, (*1)

See Alternative:

https://github.com/thecodingmachine/safe, (*2)

This is a more comprehensive library of functions that is auto generated., (*3)

I prefer to only include functions that I actually use in this library, the vast majority of functions above will never be used and so I'm reluctant to bring them all in., (*4)

Functions Replaced:

File Functions

file_get_contents

returns false|string by default, (*5)

replaced with \ts\file_get_contents, (*6)

String Functions

strpos

returns false|int by default, (*7)

multiple replacements depending on use case:, (*8)

\ts\strpos to get the actual string position when it is known that the haystack contains the needle, (*9)

\ts\stringContains to check if the haystack contains the needle, (*10)

\ts\stringStartsWith to check if the haystack begins with the needle, (*11)

stripos

//TODO - but will be as above, but case insensitive, (*12)

Array Functions

in_array

By default this is not strict and requires a third parameter of true., (*13)

Simply replace with \ts\arrayContains instead to have this handled automatically., (*14)

This improves readability and also prevents various mutation testing escapees that would be otherwise hard to catch, (*15)

Find: (|\\)in_array\((.+?),(.+?),.+?\), (*16)

Replace: \\ts\\arrayContains($2, $3), (*17)

Debug Functions

When passing true, this returns a string, for example, (*18)

<?php
$string=\print_r(['a'=>1], true);

To make this type safe, we replace with, (*19)

<?php
$string = \ts\varToString(['a'=>1]);

Reflection

ReflectionClass

Always returns a single type, (*20)

Normalises the type, for example to empty strings or throws exceptions on failure, (*21)

Find Replace

Find Replace
new \ReflectionClass new \ts\Reflection\ReflectionClass
: \ReflectionClass : \ts\Reflection\ReflectionClass
@var \ReflectionClass @var \ts\Reflection\ReflectionClass
(\ReflectionClass $ (\ts\Reflection\ReflectionClass $
@param \ReflectionClass $ @param \ts\Reflection\ReflectionClass $

The Versions

05/07 2018

dev-master

9999999-dev

Type safe wrappers around internal functions that return mixed types

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires