An open API service indexing awesome lists of open source software.

https://github.com/edmondscommerce/typesafe-functions

some wrappers around internal functions that allow things to be more strictly typed
https://github.com/edmondscommerce/typesafe-functions

Last synced: 11 months ago
JSON representation

some wrappers around internal functions that allow things to be more strictly typed

Awesome Lists containing this project

README

          

# Typesafe Functions

To assist with boilerplate around working with internal functions but ensuing that your code keeps tools like [phpstan](https://github.com/phpstan/phpstan) and [phpqa](https://github.com/edmondscommerce/phpqa) happy

## See Alternative:
https://github.com/thecodingmachine/safe

This is a more comprehensive library of functions that is auto generated.

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.

## Functions Replaced:

### File Functions

#### `file_get_contents`

returns false|string by default

replaced with `\ts\file_get_contents`

### String Functions

#### `strpos`

returns false|int by default

multiple replacements depending on use case:

`\ts\strpos` to get the actual string position when it is known that the haystack contains the needle

`\ts\stringContains` to check if the haystack contains the needle

`\ts\stringStartsWith` to check if the haystack begins with the needle

#### `stripos`

//TODO - but will be as above, but case insensitive

### Array Functions

#### `in_array`

By default this is not strict and requires a third parameter of true.

Simply replace with `\ts\arrayContains` instead to have this handled automatically.

This improves readability and also prevents various mutation testing escapees that would be otherwise hard to catch

Find: `(|\\)in_array\((.+?),(.+?),.+?\)`

Replace: `\\ts\\arrayContains($2, $3)`

### Debug Functions

#### `print_r`

When passing true, this returns a string, for example
```php
1], true);
```
To make this type safe, we replace with
```php
1]);
```

## Reflection

### ReflectionClass

Always returns a single type

Normalises the type, for example to empty strings or throws exceptions on failure

#### 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 $ ` |