2

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.

sdexp
  • 756
  • 4
  • 18
  • 36
  • 2
    did you forget `use` statements? Also, what's the namespace inside PackageName.php? – delboy1978uk Oct 11 '18 at 14:51
  • Thank you. Yes I did forget to use `use` statements but I am getting the same error. I updated the question with the top of the main file with the namespace. – sdexp Oct 11 '18 at 14:58

1 Answers1

1

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

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • I made the changes you suggest. In PackageName.php, all the classes and methods are showing as found in the IDE (PHPStorm) but I'm still getting `exception [err:Error("Class 'blah\ProcessNames' not found")] has been thrown.` I'm sure it's something very basic I'm not doing. Any ideas? – sdexp Oct 12 '18 at 08:20
  • When I do a `shouldHaveType` I get `expected an instance of blah\ProcessNames, but got [obj:blah\Packagename].` – sdexp Oct 12 '18 at 08:35
  • Take Packagename out of the composer.json namespace and run composer dumpautoload – delboy1978uk Oct 12 '18 at 08:51
  • I did this but same error, very puzzling. I'm not using the interface either. If feels like something extremely basic that I've missed out. – sdexp Oct 12 '18 at 09:01
  • so its just namespace `blah` points to `src/`? Your PackageName class has just namespace blah and is sitting in `src/PackageName`? – delboy1978uk Oct 12 '18 at 09:10
  • `blah\PackageName\ProcessNames` would be in `src/PackageName/ProcessNames.php` if it's configured like my previous comment – delboy1978uk Oct 12 '18 at 09:12
  • 2
    Ahhh... filename has to be the same as the class. Much appreciated!! – sdexp Oct 12 '18 at 09:13
  • Yes, just follow the directory structure and you'll be all good! – delboy1978uk Oct 12 '18 at 09:51