Stern PHP Type Safety: Make class function calls invoke strict versions

Recommend this page to a friend!
  Info   View files Documentation   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-06-26 (11 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 20 This week: 4All time: 9,961 This week: 61Up
Version License PHP version Categories
stern 1.0MIT/X Consortium ...5Language, PHP 7
Description Author

This package can make class function calls invoke strict versions.

It provides a trait that can be used by any class, so when mission functions of the class, it can forward the calls to functions with the prefix strict, so PHP 7 based strict type checking of the parameters values passed to the class is performed.

Innovation Award
PHP Programming Innovation award nominee
June 2020
Nominee
Vote
PHP 7 introduced the possibility to impose stricter restrictions to the types of the values passed or returned by class functions.

This package provides a simple trait that forces calling stricter versions of PHP class functions, even if applications call past versions of class versions that did not used to be stricter.

Manuel Lemos
  Performance   Level  
Name: Scott Arciszewski <contact>
Classes: 25 packages by
Country: United States United States
Age: ???
All time rank: 1384189 in United States United States
Week rank: 3 Up1 in United States United States Up
Innovation award
Innovation award
Nominee: 20x

Winner: 1x

Details

Stern

Build Status Latest Stable Version Latest Unstable Version License

Stern lets you built type-safe PHP projects, even if your project's users aren't writing type-safe code.

Requires PHP 7+

Usage

Using Stern is simply:

  1. Make your class use the SternTrait.
  2. Rename your methods from whateverName to strictWhateverName.
  3. Enjoy strict-typing whether your users like it or not.

Example

  <?php
  declare(strict_types=1);
  namespace YourVendor\YourNamespace;

  class YourClassThatUsesStrictTypes
  {
+      use \ParagonIE\Stern\SternTrait;
  
      /.../
  
-     public function foo(string $param = ''): bool
+     public function strictFoo(string $param = ''): bool
      {
      }
  }

Docblock Usability

For better usability (especially with type-aware IDEs like PHPStorm), make sure you use @method docblocks.

  <?php
  declare(strict_types=1);
  namespace YourVendor\YourNamespace;

+   /
+    * @method bool foo(string $param = '')
+    */
  class YourClassThatUsesStrictTypes
  {
+      use \ParagonIE\Stern\SternTrait;
  
      /.../
  
-     public function foo(string $param = ''): bool
+     public function strictFoo(string $param = ''): bool
      {
      }
  }
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file SternTrait.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagephpunit (1 file)
Files folder imagetest-classes (1 file)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  phpunit  
File Role Description
  Accessible without login Plain text file WeakSternTest.php Class Class source

  Files folder image Files  /  tests  /  test-classes  
File Role Description
  Accessible without login Plain text file Weak.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:20
This week:4
All time:9,961
This week:61Up

For more information send a message to info at phpclasses dot org.