0

I need to transform text file changing variable types by prepending them with specified namespace. But I need to skip base types such as array or string.

So the text

/**
 * @param ParamType $param
 * @return ReturnType
 */
public function SomeFunction(ParamType $param) {
   ...
}

/**
 * @param array $param
 * @return string
 */
public function OtherFunction($param) {
   ...
}

Should be converted to

/**
 * @param \My\Namespace\ParamType $param
 * @return \My\Namespace\ReturnType
 */
public function SomeFunction(\My\Namespace\ParamType $param) {
   ...
}

/**
 * @param array $param
 * @return string
 */
public function OtherFunction($param) {
   ...
}

My transformation now looks as follows

$classFileText = preg_replace(
    [
        '/@param\s+([A-Za-z]+)\s+\$(\w+)/',
        '/@return\s+([A-Za-z]+)/',
        '/@var\s+([A-Za-z]+)/',
        '/\s+function\s+([A-Za-z]+)\s*\(\s*([A-Za-z]+)\s+\$(\w+)/',
    ],
    [
        '@param \\' . $typesNamespace . '\\\\$1 $\\2 ',
        '@return \\' . $typesNamespace . '\\\\$1',
        '@var \\' . $typesNamespace . '\\\\$1',
        ' function $1(\\' . $typesNamespace . '\\\\$2 $\\3',
    ],
    $classFileText
);

This works, but base types are prepended as others.

I've tried to solve this using this tip: Regular expression to match a line that doesn't contain a word?

And many others... But I failed.

p.s. I came across this, making the transformation of the WSDL schema into a set of classes to access the SOAP service

Community
  • 1
  • 1
oakymax
  • 1,454
  • 1
  • 14
  • 21
  • So, where's the problem with `/@param\s+(?!(?:array|string|callable)\s)([A-Za-z]+)\s+\$(\w+)/` (complete as needed) … As shown by the other question? – bwoebi Jul 31 '16 at 23:21
  • @bwoebi i don't know, but this not works... tried to use this pattern, it works fine in refiddle: http://refiddle.com/refiddles/579e914475622d2391020000 but in my code it is not working, "array" and "string" still matched – oakymax Aug 01 '16 at 00:06
  • @bwoebi fixed, now works. looks like there was some typo in my code... many thanks! )) – oakymax Aug 01 '16 at 00:15
  • @bwoebi Oh, those regular expressions... it's just some sort of black magic. Still don't understand how it works )) – oakymax Aug 01 '16 at 00:17

1 Answers1

0

Thanks to @bwoebi for the answer in comments!

Now works fine:

$classFileText = preg_replace(
    [
        '/@param\s+(?!(?:array|string)\s)([A-Za-z]+)\s+\$(\w+)/',
        '/@return\s+(?!(?:array|string)\s)([A-Za-z]+)/',
        '/@var\s+(?!(?:array|string)\s)([A-Za-z]+)/',
        '/\s+function\s+([A-Za-z]+)\s*\(\s*(?!(?:array|string)\s)([A-Za-z]+)\s+\$(\w+)/',
    ],
    [
        '@param \\' . $typesNamespace . '\\\\$1 \$$2 ',
        '@return \\' . $typesNamespace . '\\\\$1',
        '@var \\' . $typesNamespace . '\\\\$1',
        ' function $1(\\' . $typesNamespace . '\\\\$2 \$$3',
    ],
    $classFileText
);

I would be grateful if someone explain how it works ))

Community
  • 1
  • 1
oakymax
  • 1,454
  • 1
  • 14
  • 21