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