From 5a53cc02f4002fd692342b13a03cd120bb705e35 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Fri, 23 Aug 2013 14:38:17 -0700 Subject: [PATCH] Adding coding standards changes --- readme.md | 2 +- src/Faker/Provider/Base.php | 11 ++++++++--- test/Faker/Provider/BaseTest.php | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 6df840d3..36391ea8 100644 --- a/readme.md +++ b/readme.md @@ -197,7 +197,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle ## Optional data -All formatters can be made optional by chaining `optional`. When optional, the formatter will randomly return `NULL`, which can be useful for seeding non-required fields. For example: +All formatters can be made optional by chaining `optional`. When optional, the formatter will randomly return `NULL`, which can be useful for seeding non-required fields. For example: $faker->optional->country diff --git a/src/Faker/Provider/Base.php b/src/Faker/Provider/Base.php index ffb2f3db..16980f82 100644 --- a/src/Faker/Provider/Base.php +++ b/src/Faker/Provider/Base.php @@ -191,11 +191,16 @@ class Base /** * Chainable method for making any formatter optional - * @param float $weight Set the percentage that the formatter is empty. "0" would always return null, + * @param float $weight Set the percentage that the formatter is empty. "0" would always return null, * "1" would always return the formatter * @return null or whatever the formatter would use */ - public function optional($weight = 0.5) { - return mt_rand() / mt_getrandmax() <= $weight ? $this->generator : new \Faker\NullGenerator; + public function optional($weight = 0.5) + { + // Return the formatter + if (mt_rand() / mt_getrandmax() <= $weight) return $this->generator; + + // Return NULL + else return new \Faker\NullGenerator(); } } diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 19de22f8..4be15357 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -131,13 +131,15 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r')); } - public function testOptionalChainingOfProperty() { + public function testOptionalChainingOfProperty() + { $faker = \Faker\Factory::create(); $this->assertNotNull($faker->optional(1)->randomNumber); $this->assertNull($faker->optional(0)->randomNumber); } - public function testOptionalChainingOfMethod() { + public function testOptionalChainingOfMethod() + { $faker = \Faker\Factory::create(); $this->assertNotNull($faker->optional(1)->randomNumber(4)); $this->assertNull($faker->optional(0)->randomNumber(4));