Swagger PHP and Doctrine Annotation issue - php

I am implementing Swagger-PHP for an API we've built.
Here is a brief recap:
Language: PHP5.3
Framework: FuelPHP 1.5.3
Environment: Local (served with Nginx)
Now I have an API method defined as follow:
/**
* #SWG\Api(
* path="/site/list",
* description="Get sites list",
* #SWG\Operation(...,
* #SWG\Parameter(...),
* #SWG\ResponseMessage(...),
* #SWG\ResponseMessage(...)
* )
* )
*/
public function action_run()
{
//doing stuff
}
I now try the following (from elsewhere in my application) to generate the JSON:
$swagger = new Swagger\Swagger('my/root/dir');
$swagger->getResource('/site/list', array('output' => 'json'));
And that first line here (when instanciating my Swagger class) is throwing me an error:
ErrorException [ User Warning ]: [Semantical Error] The class
"package" is not annotated with #Annotation. Are you sure this class
can be used as annotation? If so, then you need to add #Annotation to
the class doc comment of "package". If it is indeed no annotation,
then you need to add #IgnoreAnnotation("package") to the class doc
comment of class #Swagger\Annotations\Api.
Adding the #IgnoreAnnotation("package") is actually not helping.
I notice the error disappears if I remove the #package from here:
https://github.com/zircote/swagger-php/blob/master/library/Swagger/Annotations/Api.php#L28
But that's not a solution.
I'm guessing this is mainly Doctrine-related but I can't seem to figure it out.
Thanks for any tips or ideas on that matter.

Because FuelPHP has a Package class (in fuel/core/package.php), which isn’t an #Annotation the DocParser generates this warning.
Swagger-PHP uses the $docParser->setIgnoreNotImportedAnnotations(true) setting, which should prevent warnings like these.
I've reported the issue and fixed the problem but sadly the patch was rejected
Report the issue (again) to doctrine, the more people complain the faster it gets fixed 😉
As a workaround replace your DocParser.php with this version

Related

The annotation "#apiDefine" in class App\Controller\UserController was never imported

I am using both phpstan and apidoc, but i have an error on phpstan that i can't find a way to solve or ignore.
Error message "Internal error: [Semantical Error] The annotation "#apiDefine" in class App\Controller\UserController was never imported. Did you maybe forget to add a "use" statement for this annotation?
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md" cannot be ignored, use excludePaths instead.
To my understanding, apidoc doesn't need any import, but when phpstan goes on my file it does not know this annotation so it triggers an error.
In my opinion the best solutions would be to ignore this kind of error, but it say:
cannot be ignored, use excludePaths instead.
But i would like to keep this file in the phpstan analyse.
How could i fix this error or ignore it ?
I wanted to ignore the annotations from api doc, but it wasn't possible. Instead I can ignore the annotations from doctrine
/**
* #IgnoreAnnotation("apiName")
* #IgnoreAnnotation("apiDefine")
* #IgnoreAnnotation("apiGroup")
* #IgnoreAnnotation("apiParam")
* #IgnoreAnnotation("apiSuccess")
*/
class UserController extends ControllerBase
The only downside that I see is the need to add it for each files.

Why does PhpStorm not recognize my import for Method and Template being used in Annotations?

I have a symfony 3.4 project in PhpStorm 2020.2. I import the following annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
and use them, for example here:
/** #Route("/feedback/delete/{id}", name="feedbackDelete") #Method("DELETE") */
and
/** #Route("/dashboard", name="dashboard") #Template() */
Now PhpStorm tells me Import 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Method' is never used - same for Template. That is not true, but why does PhpStorm not recognize it?
I use the plugins PHP Annotations 7.1.3 and Symfony Support 0.21.202.
It's annoying because it always shows up in code analyses and I can't auto-optimize imports, because it breaks the code.
I thought, why bothering to ask - symfony 3.4 is old and I will probably soon migrate to symfony 5.X. But migration is a headache and since I recently talked with someone from a big symfony project where they still use 2.*, I thought there might actually be more people with this problem.
After experimenting a bit, I found the problem:
The PhpStorm plugin PHP Annotations does recognize the annotation, if it is starting in a new line of the PhpDoc. So while #Template is not recognized as used here:
/** #Route("/dashboard", name="dashboard") #Template() */
it works, if I change it to
/**
* #Route("/dashboard", name="dashboard")
* #Template()
*/
or even this is enough:
/** #Route("/dashboard", name="dashboard")
* #Template() */
I don't really like that since I like to keep my line count low and I don't see any advantage of putting this small annotation in a new line, but at least now I have a work-around.
I also opened an issue for this which can be found here: https://github.com/Haehnchen/idea-php-annotation-plugin/issues/211

Interface '...' not found in (PHP SQL Query Builder)

I'm trying to use PHP SQL Query Builder in a project I'm working on. I am installing it manually. I did this by copying the src directory into my project and then using the following code within my class:
$this->builder = new NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder();
The error I get whenever the builder is used is:
[18-Apr-2017 12:57:48 UTC] PHP Fatal error: Interface 'NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface' not found in /home/thomassm/public_html/php/lib/sqlbuilder/Builder/GenericBuilder.php on line 24
GenericBuilder.php
namespace NilPortugues\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
/**
* Class Generic.
*/
class GenericBuilder implements BuilderInterface
{//...}
BuilderInterface.php
namespace NilPortugues\Sql\QueryBuilder\Builder;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
/**
* Interface BuilderInterface.
*/
interface BuilderInterface
{
/**
* #param QueryInterface $query
*
* #return string
*/
public function write(QueryInterface $query);
/**
* #param QueryInterface $query
*
* #return string
*/
public function writeFormatted(QueryInterface $query);
}
I assume the error is somehow caused by the way the files are called, any suggestions?
I am installing it manually. I did this by copying the src directory into my project and then using the following code within my class: ...
Every source file should be included through include/requrie before using. But PHP allow to setup autoloading for classes, interfaces and traits. In short, when runtime meets an undefined class then a special callback invokes, which can load the relevant file.
Although formally the autoloading rules can be arbitrary, but most of the modern projects supports the common community standard: PSR-4 Autoloader. So, you need an implementation of this standard.
As said in the library description, the recommended way to install is through Composer:
php composer.phar require nilportugues/sql-query-builder
Composer provides own PSR-4 implementation and generates file vendor/autoload.php. Usually this file included at the application entry point. It allow using the classes from all required libraries.
You can use the packages without the Composer (relevant answer). But it requires a lot of work (you still need an PSR-4 autoloader) and this way is rarely used today.

Doctrine Annotation failing

Currently I'm trying to implement the Doctrine CouchDB Bundle in a Silex Application. At one point the complete site ends in a 500/internal server error in my local dev stack. Setting breakpoints and debugging them with XDebug and PHPStorm hasn't brought me to any result so far. Apache Error logs are empty, PHP error logs as well and error_reporting(-1); still doesn't give any output. Probably the problem is the reflection class usage in there.
Everything works well, until I try to use CouchDB Annotations from
use Doctrine\ODM\CouchDB\Mapping\Annotations as CouchDB;
and use them in my Document
/** #CouchDB\Document */
class Station
{
/**
* #Index
* #Id
*/
private $id;
If I remove the CouchDB\ above, everything works. But if I remove it, and use #Id(strategy="ASSIGNED"), I run into the exact same problem.
I tried to register the Annotations in several ways without luck. #Ocramius suggested in chat that I should simply override the Autoloader, which worked well with getting beyond some other problems, but not for that case (just adding it here in case someone else needs it).
AnnotationRegistry::registerLoader( function( $className ) {
return class_exists( $className );
} );
Try to use
/**
* #CouchDB\Index
* #CouchDB\Id
*/
instead of
/**
* #Index
* #Id
*/
You can also solve this by adding this line before you run your Silex Application (probably in index.php).
AnnotationRegistry::registerLoader('class_exists');

Using PHP 5.3 namespaces (Doctrine2) with Zend Framework classes

i am having a situation where my doctrine model, Post, is in the namespace Application\Entities and i want to try to implement Zend_Acl_Resource_Interface. i get the error
Fatal error: Interface
'Application\Entities\Zend_Acl_Resource_Interface'
not found in
D:\Projects\Websites\php\ZendFram
ework\LearningZF\library\Application\Entities\Post.php
on line 8
namespace Application\Entities;
use DoctrineExtensions\NestedSet\Node;
/**
* #Entity #Table(name="posts")
*/
class Post implements Node, Zend_Acl_Resource_Interface {
update
if i try
class Post implements Node, \Zend_Acl_Resource_Interface {
Fatal error: Interface
'Zend_Acl_Resource_Interface' not
found in
D:\Projects\Websites\php\ZendFramework\LearningZF\library\Application\Entities\Post.php on
line 8
Are you using the ZF 2.0 branch or the stable one, e.g. 1.10? Is your autoloader setup to load classes with the ZF naming scheme?
You can use the Conversion tool the ZF devs used to convert ZF to using namespaces:
http://github.com/ralphschindler/PHPTools
http://blog.calevans.com/2010/03/27/zends-new-namespace-converter/
As far as I remember Zend Framework does not uses namespaces (until 2.x comes out anyway) so it's classes are in global namespace. Try class Post implements Node, \Zend_Acl_Resource_Interface {
i am going to put this as an answer, so i can mark this qn as answered if noone have any other answers, its actually a workaround i used. i am not too sure why it worked exactly, or rather why the Zend autoloader failed
i added the require_once to include Zend_Acl_Resource_Interface
namespace Application\Entities;
use DoctrineExtensions\NestedSet\Node;
require_once "Zend/Acl/Resource/Interface.php";
/**
* #Entity #Table(name="posts")
*/
class Post implements Node, \Zend_Acl_Resource_Interface {
i think a better fix will be to conversion tool in #Gordon's answer. but i cant fix all the errors i get with "unclean" conversion yet. paths are broken.

Categories