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

Recommend this page to a friend!
  Info   Documentation   View files (10)   Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2020-06-26 (1 month ago) Not yet rated by the usersTotal: 32 This week: 2All time: 9,936 This week: 114
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 missing functions of the class are called, 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
Number 9
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
Picture of Scott Arciszewski
  Performance   Level  
Name: Scott Arciszewski <contact>
Classes: 25 packages by
Country: United States United States
Age: ???
All time rank: 1374186 in United States United States
Week rank: 22 4 in United States United States
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  
File Role Description
src (1 file)
tests (1 file, 2 directories)
.travis.yml Data Auxiliary data
composer.json Data Auxiliary data
LICENSE Lic. License text
phpunit.xml.dist Data Auxiliary data
psalm.xml Data Auxiliary data
README.md Doc. Read me

  Files  /  src  
File Role Description
   SternTrait.php Class Class source

  Files  /  tests  
File Role Description
phpunit (1 file)
test-classes (1 file)
   autoload.php Aux. Auxiliary script

  Files  /  tests  /  phpunit  
File Role Description
   WeakSternTest.php Class Class source

  Files  /  tests  /  test-classes  
File Role Description
   Weak.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:32
This week:2
All time:9,936
This week:114

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