/** * surround a string with braces, if it would need braces in order to be used * as a PHP property name in eval() * * @param string $propertyName * the property name to add braces to (if required) * @return string */ function quote_property($propertyName) { // robustness! if (!is_stringy($propertyName)) { throw new InvalidArgumentException('$propertyName is not a valid class or object property name'); } // would the string need braces if we used it in eval()? // // the rules are: // - first character must be an ASCII letter or an underscore // - the remaining characters can only be ASCII letters, numbers, // or an underscore // // any string that doesn't satisfy those rules needs braces adding if (!preg_match("/[A-Za-z_][A-Za-z0-9_]{" . (strlen($propertyName) - 1) . "}/", $propertyName)) { // yes it would return '{' . $propertyName . '}'; } // if we get here, then no action is needed return $propertyName; }
/** * @covers ::is_stringy */ public function test_returns_FALSE_for_resources() { // ---------------------------------------------------------------- // setup your test $item = STDIN; $expectedResult = false; // ---------------------------------------------------------------- // perform the change $actualResult = is_stringy($item); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }