phpcomposer-phpphpspec

File structure in a PHP composer package with PHPSpec


I had a working composer package with a single files and a single class. So, now I'm trying to change the package so that it is more like SOLID.

I have a file structure like this...

PackageName.php
addresses.php
names.php
interfaces
    names.php
    addresses.php

When I use PHPSpec the methods in the main PackageName.php are getting validated but within one of the methods I have something like...

namespace blah\PackageName;

use blah\PackageName\ProcessNames;

class PackageName
{
    public function formatData($user)
    {
        $place_holders = array();
        $place_holders = ProcessNames::process_name($user, $place_holders);
        $place_holders = ProcessAddresses::process_address($user, $place_holders);
        return json_encode($place_holders);
    }
}

which gives the error...

48 ! should do the address exception [err:Error("Class 'blah\PackageName\ProcessNames' not found")] has been thrown.

The composer.json is like...

{
    "name": "blah\PackageName",
    "description": "Format data.",
    "require": {
        "nesbot/carbon": "^1.34",
        "php": ">7.0.0"
    },
    "require-dev": {
        "phpspec/phpspec": "^4.3"
    },
    "authors": [
        {
            "name": "me",
            "email": "me@emailaddress.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "blah\\PackageName\\": "src/",
            "spec\\blah\\PackageName\\": "spec/"
        },
        "files": {
            "src/interfaces/names.php",
            "src/names.php"
        }
    }
}

I can't see how to include files in the package. I'm not sure I need the "files" part on composer.json but I'm trying to figure out how to do it. Any info much appreciated.


Solution

  • This is the same issue someone else had today! A simple error.

    Replace:

    namespace blah\PackageName;
    

    with:

    namespace blah;
    

    The full class name includes the namespace and class name. So essentially your class was actually an instance of blah\PackageName\Packagename

    Possibly take Packagename out of the composer.json, depending on your needs, and if you change that, remember to run composer dumpautoload